pythonadvanced

Stream OpenAI Responses with Tool Calls

Handle streaming responses that include tool calls by accumulating delta chunks from the OpenAI API.

python
from openai import OpenAI
import json

client = OpenAI()

tools = [{'type':'function','function':{'name':'search','description':'Search the web','parameters':{'type':'object','properties':{'query':{'type':'string'}},'required':['query']}}}]

stream = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role':'user','content':'Search for the latest Python news'}],
    tools=tools,
    stream=True,
)

collected_chunks = []
tool_call_args   = ''
tool_call_name   = ''

for chunk in stream:
    delta = chunk.choices[0].delta
    if delta.content:
        print(delta.content, end='', flush=True)
    if delta.tool_calls:
        tc = delta.tool_calls[0]
        if tc.function.name:
            tool_call_name += tc.function.name
        if tc.function.arguments:
            tool_call_args += tc.function.arguments

if tool_call_name:
    args = json.loads(tool_call_args)
    print(f'\nTool call: {tool_call_name}({args})')
    print('Simulated result: Latest Python news: Python 3.14 alpha released.')

Use Cases

  • streaming tool calls
  • delta accumulation
  • real-time agents

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.