Documentation
¶
Overview ¶
Package toolcall provides LLM tool call argument reassembly from streaming chunks. Handles incremental tool_calls[i].function.arguments fragments from OpenAI-compatible APIs (OpenAI, DeepSeek, GLM, Kimi, Qwen, Grok, Doubao, Gemini) and produces complete, validated ToolCall objects.
Designed for extraction to github.com/brightman-ai/kit/toolcall.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Assembler ¶
type Assembler struct {
// contains filtered or unexported fields
}
Assembler accumulates incremental tool call deltas by index and produces complete ToolCall objects. It handles:
- Split function.name across deltas (rare but spec-legal)
- Incremental function.arguments concatenation (the common case)
- Parallel tool calls with different indices
- Out-of-order delta delivery (by index)
Usage:
a := toolcall.NewAssembler()
for chunk := range stream {
for _, delta := range chunk.ToolCalls {
a.Feed(delta)
}
}
completed := a.Complete()
func (*Assembler) Complete ¶
Complete returns all accumulated tool calls sorted by index. Call this after the stream ends (finish_reason = "tool_calls" or "stop"). Only entries with a non-empty function name are returned.
func (*Assembler) Feed ¶
Feed processes a single tool call delta. Call this for each delta in each streaming chunk's tool_calls array.
type Delta ¶
type Delta struct {
Index int `json:"index"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Function struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"` // incremental JSON fragment
} `json:"function,omitempty"`
}
Delta represents an incremental tool call fragment from a streaming LLM response. In OpenAI-compatible APIs, tool_calls arrive as deltas:
- First delta for an index: carries ID + function.name (+ possibly partial arguments)
- Subsequent deltas: carry only function.arguments fragments to concatenate
type ToolCall ¶
type ToolCall struct {
Index int `json:"index"`
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunction `json:"function"`
}
ToolCall is a fully reassembled tool call with complete arguments.
type ToolCallFunction ¶
type ToolCallFunction struct {
Name string `json:"name"`
Arguments string `json:"arguments"` // complete JSON string
}
ToolCallFunction holds the function name and complete arguments JSON.