stream = client.run_local(
model="gpt-4o",
messages=[{"role": "user", "content": "What is the weather in Tokyo?"}],
stream=True,
tools=[{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
},
"required": ["location"],
},
}],
)
# Text chunks still stream as normal
for chunk in stream:
print(chunk, end="")
result = stream.result.result()
# Handle tool calls
if result.finish_reason == "tool_calls":
tool_call = result.tool_calls[0]
weather_data = get_weather(tool_call.arguments["location"])
# Continue conversation with tool result
follow_up = client.run_local(
model="gpt-4o",
messages=[
{"role": "user", "content": "What is the weather in Tokyo?"},
result.message, # Assistant's message with tool calls
{
"role": "tool",
"tool_call_id": tool_call.id,
"tool_name": tool_call.name,
"content": json.dumps(weather_data),
},
],
stream=True,
tools=[...], # same tools
)
for chunk in follow_up:
print(chunk, end="")