Skip to content

Python Responses tools with Poetry

Responses API + local tool loop

This example shows a minimal but real function-calling flow on OpenAI /v1/responses through LunarGate. The app executes a local tool, sends function_call_output, and lets the model finish with a final assistant message.

Use this example when:

  • you already call OpenAI Responses API and want the smallest LunarGate-compatible version
  • you need one clear tool-calling loop before building larger agent behavior
  • you want to verify /v1/responses routing and headers in your gateway setup

What it demonstrates

  • client.responses.with_raw_response.create(...) against LunarGate through the openai Python library
  • a local tool definition (get_current_time)
  • handling function_call items from model output
  • sending function_call_output back to the model
  • preserving previous_response_id across follow-up turns through LunarGate
  • printing selected X-LunarGate-* headers from the raw response

Run it

cp .env.example .env
poetry install

Start the gateway in a separate terminal:

cp config-simple.yaml.example config.yaml
lunargate --config ./config.yaml

Then run the demo:

poetry run python main.py

What to inspect

  • main.py for the full function-calling loop
  • config-simple.yaml.example for explicit /v1/responses route matching with upstream_request_type: responses
  • .env.example for LUNARGATE_MODEL, LUNARGATE_BASE_URL, and API key values

Important implementation detail

The example still uses the official openai Python library end to end.

It intentionally reads JSON from the OpenAI raw-response wrapper instead of using the typed Responses parser, because the typed parser currently crashes on this Python/SDK combination for this flow. The request path and payload shape are still the real OpenAI Responses API shape.

Why this example matters

Many examples stop at "the model requested a tool". This one continues through the full loop and shows the handoff back to the model, which is the part teams often miss in first implementations.