Setup and execution
Use Python 3.10+ with the standard library; no provider SDK is required. Set your gateway token inAPI_KEY, then use a root URL without /v1 or /v1beta. Deployment path prefixes are supported.
examples/ if you saved the file separately. The first command requests a greeting; the second offers multiply, asks for 7 × 8, and returns the calculation to the model for its final answer.
API_NATIVE_MODE defaults to text. In tools mode, a model may also answer directly. Test responses are fixed fixtures; real wording and tool selection are not guaranteed.
Events and the tool loop
When a call supplies an ID, the example echoes it unchanged and rejects empty or repeated IDs. Without IDs, it associates calls and results by order and name, without inventing IDs. Repeated calls to the same function each receive their own result.
Every thought signature remains on its original part, including empty text and signature-only carriers. Parts are neither merged nor flattened. Thoughts are hidden only from terminal display, not removed from replay history. Verify signature forwarding on your actual channel.
This example uses complete function argument objects and does not enable fine-grained argument streaming. It fails on
partialArgs or willContinue: true instead of treating them as complete calls. It does not concatenate arbitrary JSON strings as Claude does. Complete calls can arrive in separate SSE frames; execution waits for candidate completion. Bare model ID restrictions match the introductory example.
Complete Python example
Failure and resource boundaries
The SSE reader handles incremental UTF-8, LF / CRLF / CR, comments, and multiline data. It ignores SSE id/retry fields rather than replaying a request automatically. Socket operations time out after 60 seconds; this is not a deadline for the entire workflow. Each turn allows at most 512 data events, 4096 lines, and 1,048,576 decoded characters, with 65,536 characters per line. These are example safeguards, not gateway limits. Only the local pure calculationmultiply is allowed. Arguments must contain exactly finite numeric a and b; booleans, extra fields, and nonfinite results fail. At most three model requests and four calls per turn are allowed. A third turn requesting more tools fails. All results are prepared before another request. Tools with side effects need separate authorization, idempotent execution, and recovery; multiplication is not a transaction design.
HTTP failures, redirects, wrong content types, malformed JSON, truncated SSE, broken connections, unsuccessful completion, and unsupported extensions exit nonzero. Credentials, upstream error bodies, thinking text, and incomplete answers are not printed. Unknown events or blocks fail explicitly until the parser is extended to support them.
Gemini additionally limits each turn to 256 parts and serialized arguments to 16,384 characters. Fine-grained argument fragments, multiple candidates, media, Live API, and asynchronous tool scheduling are outside this example.
Verification
Runnpm run check:examples. Tests execute this script against a local HTTP fixture, covering text/tool modes, complete history and signatures, result association, split UTF-8, errors, disconnects, no retries, and second-turn failure short-circuiting. No real models are called; actual event delivery and signature preservation require deployment testing.
Gemini native API · Signature preservation
阅读中文