Skip to content

examples: add a streaming tool-calling example#650

Open
Rohan5commit wants to merge 1 commit into
openai:mainfrom
Rohan5commit:examples/streaming-tool-calls-20260427
Open

examples: add a streaming tool-calling example#650
Rohan5commit wants to merge 1 commit into
openai:mainfrom
Rohan5commit:examples/streaming-tool-calls-20260427

Conversation

@Rohan5commit

Copy link
Copy Markdown
Contributor

Summary

  • add a dedicated streaming example that accumulates tool call chunks and then sends the tool result back to the model
  • keep the example in examples/, which CONTRIBUTING.md marks as a safe manually maintained surface

Related issue

Guideline alignment

  • keeps the change to a single example file with no library behavior changes
  • uses the existing chat-completions example style and focuses on one concrete streaming tool-calling flow

Validation

  • ran git diff --check

Copilot AI review requested due to automatic review settings April 27, 2026 02:43
@Rohan5commit Rohan5commit requested a review from a team as a code owner April 27, 2026 02:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.go showcasing a full streaming → tool execution → follow-up completion flow
  • Demonstrate use of ChatCompletionAccumulator with JustFinishedToolCall() 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

Copilot AI Apr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
continue
panic(fmt.Sprintf("unexpected tool call %q (id=%s)", toolCall.Function.Name, toolCall.ID))

Copilot uses AI. Check for mistakes.
for stream.Next() {
chunk := stream.Current()
if !acc.AddChunk(chunk) {
panic("failed to accumulate stream chunk")

Copilot AI Apr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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))

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Want an example of streaming with tool calling

2 participants