Streaming Responses
Use get_streaming_response(...) when you want incremental output instead of waiting for the full response.
Basic Streaming
from ToolAgents import ToolRegistry
from ToolAgents.agents import ChatToolAgent
from ToolAgents.data_models.messages import ChatMessage
from ToolAgents.provider import OpenAIChatAPI
api = OpenAIChatAPI(api_key="your-api-key", model="gpt-4o-mini")
agent = ChatToolAgent(chat_api=api)
settings = api.get_default_settings()
tool_registry = ToolRegistry()
messages = [
ChatMessage.create_system_message("You are a helpful assistant."),
ChatMessage.create_user_message("Tell me about quantum computing."),
]
for chunk in agent.get_streaming_response(
messages=messages,
settings=settings,
tool_registry=tool_registry,
):
print(chunk.chunk, end="", flush=True)
Chunk Fields
ChatResponseChunk exposes:
chunkhas_tool_calltool_callhas_tool_call_resulttool_call_resultfinishedfinished_response
Example:
for chunk in agent.get_streaming_response(
messages=messages,
settings=settings,
tool_registry=tool_registry,
):
print(chunk.chunk, end="", flush=True)
if chunk.has_tool_call:
print("\nTool call:", chunk.get_tool_name())
if chunk.has_tool_call_result:
print("\nTool result:", chunk.get_tool_results())
if chunk.finished:
final_response = chunk.finished_response
Streaming with Chat History
from ToolAgents.data_models.chat_history import ChatHistory
chat_history = ChatHistory()
chat_history.add_system_message("You are a helpful assistant.")
chat_history.add_user_message("What is 42 * 8?")
final_response = None
for chunk in agent.get_streaming_response(
messages=chat_history.get_messages(),
settings=settings,
tool_registry=tool_registry,
):
print(chunk.chunk, end="", flush=True)
if chunk.finished:
final_response = chunk.finished_response
if final_response is not None:
chat_history.add_messages(final_response.messages)
Async Streaming
Use AsyncChatToolAgent for async applications:
import asyncio
from ToolAgents.agents import AsyncChatToolAgent
async def main(agent, messages, settings, tool_registry):
async for chunk in agent.get_streaming_response(
messages=messages,
settings=settings,
tool_registry=tool_registry,
):
print(chunk.chunk, end="", flush=True)
asyncio.run(main(agent, messages, settings, tool_registry))
Best Practices
- Read
chunk.chunkfor user-visible text. - Use
finished_responseas the canonical final result. - Append
finished_response.messagesback into chat history after the stream completes. - Watch
has_tool_callandhas_tool_call_resultif your UI surfaces tool activity.