Inference Logoinference.sh

Streaming

Handle real-time message updates and tool calls.

Note: Agent streaming sends the full message content on each update, not incremental chunks. You don't need to concatenate—each update gives you the complete message so far.


Basic Streaming

python
1agent = client.agent({...})23for message in agent.send_message("Tell me a story", stream=True):4    print(message.text)  # Full message content so far

Message Callbacks

Handle streaming with callbacks:

python
1def on_message(text, done):2    if done:3        print(f"\n[Complete] Final: {text}")4    else:5        print(f"Current message: {text}")67agent.send_message(8    "Tell me a story",9    on_message=on_message10)

Tool Call Callbacks

Handle tool invocations:

python
1def on_tool_call(tool_call):2    print(f"Calling: {tool_call.name}")3    print(f"Args: {tool_call.arguments}")4    # Tool is executed automatically if handler is defined56agent.send_message(7    "What's 42 * 17?",8    on_tool_call=on_tool_call9)

Manual Tool Handling

For tools without handlers:

python
1calculator = (2    tool("calculator")3    .describe("Do math")4    .param("expression", string("Expression"))5    .build()  # No handler6)78agent = client.agent({...})910response = agent.send_message("What's 42 * 17?")1112# Check for pending tool calls13if response.pending_tool_calls:14    for tc in response.pending_tool_calls:15        result = eval(tc.arguments["expression"])16        agent.submit_tool_result(tc.id, str(result))

Combined Callbacks

python
1def on_message(text, done):2    if done:3        print(f"\nFinal: {text}")4    else:5        print(f"Progress: {text[:50]}...")67def on_tool_call(tool_call):8    print(f"\n[Tool: {tool_call.name}]")910response = agent.send_message(11    "Generate an image of a sunset",12    on_message=on_message,13    on_tool_call=on_tool_call14)

Async Client

For async/await, use AsyncInference:

python
1from inferencesh import AsyncInference23async def main():4    client = AsyncInference(api_key="inf_...")5    agent = client.agent({...})6    7    for message in agent.send_message("Hello", stream=True):8        print(message.text)910asyncio.run(main())

we use cookies

we use cookies to ensure you get the best experience on our website. for more information on how we use cookies, please see our cookie policy.

by clicking "accept", you agree to our use of cookies.
learn more.