Skip to content

fix anthropic streaming tool index increment #1284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

narengogi
Copy link
Collaborator

closes #1275

For multiple tool calls, the tool index was not getting updated

Copy link

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

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

Good fix for tool index initialization, but the increment logic could be more explicit and consistent.

@narengogi narengogi force-pushed the fix/anthropic-tools-index-streaming branch from 685e4e5 to 0169749 Compare August 14, 2025 12:37
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@Portkey-AI Portkey-AI deleted a comment from matter-code-review bot Aug 14, 2025
@narengogi narengogi force-pushed the fix/anthropic-tools-index-streaming branch from 0169749 to ba7e393 Compare August 14, 2025 12:39
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@narengogi narengogi requested review from VisargD and b4s36t4 August 14, 2025 12:39
@Portkey-AI Portkey-AI deleted a comment from matter-code-review bot Aug 14, 2025
@narengogi narengogi force-pushed the fix/anthropic-tools-index-streaming branch from ba7e393 to 7cb9360 Compare August 14, 2025 12:41
Copy link

Code Quality bug fix

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This pull request addresses an issue where the toolIndex was not correctly updated for multiple tool calls in streaming responses from Anthropic and Google Vertex AI providers. It introduces an initialization of streamState.toolIndex to -1 if it's undefined and simplifies the increment logic to streamState.toolIndex + 1 when a tool_use content block starts.

🔍 Impact of the Change

This ensures accurate and consistent indexing of tool calls during streaming, preventing off-by-one errors and improving the reliability of applications that process multiple tool calls from these streaming APIs.

📁 Total Files Changed

  • src/providers/anthropic/chatComplete.ts: Modified AnthropicChatCompleteStreamChunkTransform to correctly initialize and increment streamState.toolIndex.
  • src/providers/google-vertex-ai/chatComplete.ts: Modified VertexAnthropicChatCompleteStreamChunkTransform to correctly initialize and increment streamState.toolIndex.

🧪 Test Added

N/A

🔒Security Vulnerabilities

N/A

Motivation

This change is needed to correctly handle the toolIndex in streaming responses from Anthropic and Google Vertex AI, ensuring proper identification and sequencing of tool calls, specifically for multiple tool calls where the index was not getting updated.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

closes #1275

Tip

Quality Recommendations

  1. Add unit tests to verify the toolIndex increment logic for multiple tool calls in streaming scenarios for both Anthropic and Google Vertex AI transformers.

  2. Consider adding a comment explaining the -1 initialization of toolIndex to clarify its purpose in the indexing sequence.

Tanka Poem ♫

Index now counts,
Tools in stream, a clear path.
No more lost numbers,
Science flows, precise and true,
Bug fixed, a small victory. 🔬✨

Sequence Diagram

sequenceDiagram
    participant T as StreamTransformer
    participant RC as ResponseChunk
    participant PC as ParsedChunk
    participant SS as StreamState

    RC->>T: processChunk(responseChunk, streamState)
    Note over T: AnthropicChatCompleteStreamChunkTransform / VertexAnthropicChatCompleteStreamChunkTransform

    alt streamState.toolIndex is undefined
        T->>SS: streamState.toolIndex = -1
    end

    T->>PC: parseChunk(responseChunk)
    Note over T: Check for 'content_block_start' and 'tool_use'

    alt isToolBlockStart (parsedChunk.type === 'content_block_start' && parsedChunk.content_block?.type === 'tool_use')
        T->>SS: streamState.toolIndex = streamState.toolIndex + 1
    end
    T-->>RC: transformedChunk
Loading

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

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.

[anthropic] invalid tool calls
2 participants