examples: add a streaming tool-calling example#650
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new Go example demonstrating streaming Chat Completions with tool calling, including accumulating streamed tool-call argument chunks and then sending tool results back to the model for a final response.
Changes:
- Add
examples/chat-completion-tool-calling-streaming/main.goshowcasing a full streaming → tool execution → follow-up completion flow - Demonstrate use of
ChatCompletionAccumulatorwithJustFinishedToolCall()to detect completed tool calls during streaming
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| params.Messages = append(params.Messages, acc.Choices[0].Message.ToParam()) | ||
| for _, toolCall := range acc.Choices[0].Message.ToolCalls { | ||
| if toolCall.Function.Name != "get_weather" { | ||
| continue |
There was a problem hiding this comment.
The loop skips any tool calls whose name isn’t get_weather, but still proceeds to make the follow-up Chat.Completions.New call. If the model ever returns an unexpected tool call (or if this example is later extended with additional tools), this will send an assistant message containing unresolved tool calls and can cause the next request to fail (e.g., missing tool result for a tool_call_id). Prefer failing fast (panic/return with an error) for unknown tool names, or ensure you append a tool result message for every tool call in acc.Choices[0].Message.ToolCalls.
| continue | |
| panic(fmt.Sprintf("unexpected tool call %q (id=%s)", toolCall.Function.Name, toolCall.ID)) |
| for stream.Next() { | ||
| chunk := stream.Current() | ||
| if !acc.AddChunk(chunk) { | ||
| panic("failed to accumulate stream chunk") |
There was a problem hiding this comment.
acc.AddChunk(chunk) can return false when a mismatch is detected (e.g., chunk IDs changing mid-stream). Panicking with a generic string makes debugging harder; consider including relevant context (e.g., accumulated ID vs chunk.ID) or panicking with a formatted error so users can quickly understand what went wrong.
| panic("failed to accumulate stream chunk") | |
| panic(fmt.Sprintf("failed to accumulate stream chunk: accumulator ID %q does not match chunk ID %q", acc.ID, chunk.ID)) |
Summary
examples/, whichCONTRIBUTING.mdmarks as a safe manually maintained surfaceRelated issue
Guideline alignment
Validation
git diff --check