Skip to main content

Module stream

Module stream 

Source
Expand description

Server-Sent Events (SSE) streaming support.

§Streaming Function Calls

When streaming responses that include function/tool calls, the arguments arrive as deltas across multiple chunks. You must accumulate the argument strings manually:

use futures_util::StreamExt;
use std::collections::HashMap;

let mut stream = client.responses()
    .create("grok-4")
    .user("What's the weather in NYC?")
    .tool(xai_rust::chat::tool(
        "get_weather", "Get weather",
        serde_json::json!({"type": "object", "properties": {"city": {"type": "string"}}}),
    ))
    .stream()
    .await?;

let mut tool_args: HashMap<String, String> = HashMap::new();

while let Some(chunk) = stream.next().await {
    let chunk = chunk?;
    for tc in &chunk.tool_calls {
        if let Some(ref func) = tc.function {
            tool_args.entry(tc.id.clone())
                .or_default()
                .push_str(&func.arguments);
        }
    }
}

Structs§

ResponseStream
A stream of response chunks from the API.