Skip to content

[NativeTool] Phase 4: Streaming tool_use (deferred) #944

@bug-ops

Description

@bug-ops

Summary

chat_with_tools() is currently non-streaming. The entire model response must complete before tool calls are parsed and dispatched. For long-running model reasoning, the user sees no output until completion.

Claude API supports streaming with tool_use (SSE content_block_start with type: tool_use). OpenAI also supports streaming function calls.

Proposed Approach

New trait method

fn chat_with_tools_stream(
    &self,
    messages: Vec<Message>,
    tools: Vec<ToolDefinition>,
    options: &ChatOptions,
) -> Pin<Box<dyn Stream<Item = Result<ChatStreamEvent>> + Send + '_>>;

ChatStreamEvent enum (new)

pub enum ChatStreamEvent {
    TextChunk(String),
    ToolCallStart { id: String, tool_id: String },
    ToolCallDelta { id: String, input_delta: String },
    ToolCallEnd { id: String },
}

Agent loop changes

  • Process stream events in real-time
  • Display TextChunk events immediately to the channel
  • Accumulate ToolCall* events into complete tool calls
  • After stream ends, dispatch accumulated tool calls

Acceptance Criteria

  • Text chunks displayed in real-time during model reasoning
  • Tool calls extracted correctly from stream
  • Multi-turn tool loops work with streaming
  • Fallback to non-streaming when provider does not support streaming tool_use

Trade-offs

  • (+) Much better UX — user sees model "thinking" text before tool calls
  • (-) Significant implementation complexity (different SSE formats per provider)
  • (-) Ollama tool streaming may not be supported by ollama_rs
  • (-) Increases test surface substantially

Status

Deferred to post-1.0. Implement only after Phases 1-3 are stable and if UX feedback demands it.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions