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. Installopenai and anthropic in your project’s Python virtual environment, then replace the placeholders in the same terminal:
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.
- Chat Completions
- Responses
- Messages
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 oneget_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.- Chat Completions
- Responses
- Messages
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.Related
- SDK setup and authentication
- API reference overview
- Output token limits
- Structured JSON and Pydantic
- Timeout settings
- Errors and limits

