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/responsesrouting and headers in your gateway setup
What it demonstrates¶
client.responses.with_raw_response.create(...)against LunarGate through theopenaiPython library- a local tool definition (
get_current_time) - handling
function_callitems from model output - sending
function_call_outputback to the model - preserving
previous_response_idacross follow-up turns through LunarGate - printing selected
X-LunarGate-*headers from the raw response
Run it¶
Start the gateway in a separate terminal:
Then run the demo:
What to inspect¶
main.pyfor the full function-calling loopconfig-simple.yaml.examplefor explicit/v1/responsesroute matching withupstream_request_type: responses.env.exampleforLUNARGATE_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.
Related docs¶
- Use Python basic with Poetry first if you want the absolute minimum non-tool setup.
- Use Python
lunargate/autodemo if you want dynamic tier routing on top of tool calls. - Keep HTTP API reference open for endpoint behavior and payload structure.