Skip to main content
Enable streaming with stream: true and define client tools with the schema required by your API. Streaming and tool calling support varies by model; test a small request with your Model ID before using either feature in an application.

Prerequisites

First complete a plain text request using the SDK examples. Copy an exact Model ID from the model plaza for the protocol you will test. The model selection guide explains what to check. Install openai and anthropic in your project’s Python virtual environment, then replace the placeholders in the same terminal:
Each Python block below runs independently. The 120-second timeout is an example for these short tests; choose your application’s timeout using the timeout guidance. Check the model’s output token limits before setting max_tokens or max_output_tokens.

Stream text responses

Streaming sends server-sent events (SSE) over one HTTP response. An SSE event is not necessarily a complete sentence or JSON argument object. The SDK parses SSE framing for you. These examples print text deltas and completion information; applications should also handle error events.

Define a weather tool for each protocol

The model requests a function call; your application validates the arguments, runs the function, and returns the result. The examples use one get_weather function with a location argument and return fixed demonstration data, not live weather. No weather service or additional Key is required. Support varies by model and protocol. Test this small tool request with your Model ID even if plain text already works. A tool declaration does not enable hosted search or other provider-run tools.

Run a tool call and return its result

These non-streaming examples show one tool round trip so you can inspect complete arguments. Each handles every tool call in the first response and allows one follow-up request. A longer agent workflow needs its own bounded loop and stop condition.
Keep the complete assistant response before adding tool results. For Responses, retain all output items, including reasoning items when returned. For Messages, preserve content blocks and their order. Match results to the returned call IDs rather than inventing IDs.

Common questions

Can I execute tool arguments as they arrive?

Accumulate fragments by tool-call index or item ID, then parse and validate the completed arguments. Partial JSON may not parse. Start with the non-streaming examples above before combining streaming and tools.

Why does plain text work while tools fail?

Support varies by model and protocol; verify a small tool request with your Model ID. Check the tool schema, returned arguments, result ID, and message order. For typed JSON validation, use the structured output FAQ.

Should I retry an interrupted stream?

Check the request record before resending. A disconnect does not establish whether the request finished. A retry starts another attempt and must not repeat a tool action that already ran. See errors and retry guidance.

Official references

These references define upstream protocol fields. Verify the feature with your BetterToken Model ID before using it.