Skip to content

fix multi turn tool calling for anthropic when arguments to the tool call is an empty object #1104

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

Merged

Conversation

narengogi
Copy link
Collaborator

@narengogi narengogi commented May 16, 2025

This PR fixes

  • multi turn tool calling when arguments are an empty object
  • expanded support for input_schema for sending $defs alongwith your schema

#1103

Testing done
Did multi turn tool calling with empty string arguments in assistant tool call message

payload for testing

{
    "model": "claude-3-5-sonnet-20240620",
    "max_tokens": 200,
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "Based on the temperature and time in California what do you suggest I wear?"
        },
        {
            "role": "assistant",
            "content": "Certainly! I'd be happy to provide you with the current temperature and time in San Francisco. To get this information, I'll need to use two separate tools. Let me fetch that data for you.",
            "tool_calls": [
                {
                    "id": "toolu_01BspjPLY4dtHkKGurn8q9A6",
                    "type": "function",
                    "function": {
                        "name": "get_current_temperature",
                        "arguments": ""
                    }
                },
                {
                    "id": "toolu_014jEfKqGbfFvRaKfiauxgPv",
                    "type": "function",
                    "function": {
                        "name": "get_current_time",
                        "arguments": "{\"location\":\"San Francisco, CA\"}"
                    }
                }
            ]
        },
        {
            "role": "tool",
            "content": "15 degrees",
            "tool_call_id": "toolu_01BspjPLY4dtHkKGurn8q9A6"
        },
        {
            "role": "tool",
            "content": "10 PM",
            "tool_call_id": "toolu_014jEfKqGbfFvRaKfiauxgPv"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_current_temperature",
                "description": "Get the current temperature for a specific location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g., San Francisco, CA"
                        },
                        "unit": {
                            "type": "string",
                            "enum": [
                                "Celsius",
                                "Fahrenheit"
                            ],
                            "description": "The temperature unit to use. Infer this from the user's location."
                        }
                    },
                    "required": [
                        "location",
                        "unit"
                    ]
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "get_current_time",
                "description": "Get the current time for a specific location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g., San Francisco, CA"
                        }
                    },
                    "required": [
                        "location"
                    ]
                }
            }
        }
    ]
}

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

Copy link

Code Quality bug fix

Summary By MatterAI MatterAI logo

🔄 What Changed

  • Added null-safe parsing for Anthropic tool call arguments
  • Implemented fallback to empty object if no arguments exist

🔍 Impact of the Change

  • Prevents potential runtime errors during multi-turn tool calling
  • Improves robustness of tool argument processing

📁 Total Files Changed

  • 1 file: src/providers/anthropic/chatComplete.ts

🧪 Test Added

  • N/A (No specific tests added with this PR)

🔒 Security Vulnerabilities

  • Mitigated potential JSON parsing errors
  • Added defensive programming technique

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes

Sequence Diagram

sequenceDiagram
participant Client
participant AnthropicChatComplete
participant ToolCall

Client->>AnthropicChatComplete: Invoke chat completion
AnthropicChatComplete->>ToolCall: Process tool calls
Note over ToolCall: Check tool function arguments length
alt Arguments exist
    ToolCall->>ToolCall: JSON.parse(arguments)
else No arguments
    ToolCall->>ToolCall: Return empty object {}
end
AnthropicChatComplete-->>Client: Return processed tool calls
Loading

@narengogi narengogi changed the title fix multi turn tool calling for anthropic when arguments to the tool … fix multi turn tool calling for anthropic when arguments to the tool call is an empty object May 16, 2025
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

Copy link

Code Quality bug fix

Summary By MatterAI MatterAI logo

🔄 What Changed

  • Fixed multi-turn tool calling for Anthropic when tool call arguments are an empty object
  • Added handling for empty argument scenarios in tool function calls

🔍 Impact of the Change

  • Resolves potential edge case in tool argument parsing
  • Improves robustness of Anthropic provider integration

📁 Total Files Changed

  • 1 file: src/providers/anthropic/chatComplete.ts

🧪 Test Added

  • N/A (No explicit test cases mentioned)

🔒 Security Vulnerabilities

  • No direct security vulnerabilities detected

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes

Sequence Diagram

sequenceDiagram
participant Anthropic as A
participant ToolCall as TC
participant ChatComplete as CC

A->>TC: Prepare Tool Call
TC-->>CC: Pass Tool Call Arguments
CC->>TC: Parse Arguments
Note over CC: Handle Empty Object Case
TC->>A: Return Processed Tool Call
Loading

@narengogi narengogi requested a review from VisargD May 27, 2025 09:41
Copy link

Code Quality bug fix

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This pull request addresses a bug in handling multi-turn tool calls for Anthropic models, specifically when the arguments to a tool call are an empty object. The transformAssistantMessage function has been updated to correctly parse tool call arguments. Previously, JSON.parse would fail if toolCall.function.arguments was an empty string. The fix introduces a conditional check: if toolCall.function.arguments has a length, it attempts to parse it; otherwise, it defaults to an empty object {}. Additionally, the AnthropicTool interface and AnthropicChatCompleteConfig have been extended to include a $defs property for tool function parameters, enhancing schema definition capabilities.

🔍 Impact of the Change

This change fixes a critical bug that prevented successful multi-turn tool calling with Anthropic when tool arguments were empty, improving the robustness and reliability of the integration. It ensures that tool calls with empty argument objects are correctly processed, preventing parsing errors and allowing the conversation flow to continue as expected. The $defs addition also improves the expressiveness of tool definitions.

📁 Total Files Changed

1 file changed.

🧪 Test Added

No explicit unit or integration tests were mentioned in the PR description. However, it is assumed that manual testing was performed to verify the fix for the specific scenario of empty tool call arguments.

🔒Security Vulnerabilities

No new security vulnerabilities were detected in the changes. The fix improves robustness by handling a specific input case more gracefully.

Motivation

This change is needed to fix a bug where multi-turn tool calling for Anthropic fails when arguments to the tool call is an empty object, as reported in #1103.

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 (Assumed to verify the bug fix)

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 (Manual testing is assumed)
  • New and existing unit tests pass locally with my changes (Assumed based on successful bug fix)

Related Issues

#1103

totalScore: 8

Tip

Quality Recommendations

  1. Consider adding a try-catch block around JSON.parse(toolCall.function.arguments) to gracefully handle cases where the arguments string might be malformed JSON, even if not empty. While the current fix addresses the empty object case, robust error handling for invalid JSON inputs could prevent runtime crashes.

  2. Ensure that the $defs addition to AnthropicTool and AnthropicChatCompleteConfig is fully utilized and documented, as it enhances the tool schema definition and could impact future tool development or integration.

Sequence Diagram

sequenceDiagram
    participant User as User/Client
    participant Gateway as Portkey Gateway
    participant AnthropicAPI as Anthropic API

    User->>Gateway: Send Chat Completion Request (with tool definitions)
    Gateway->>AnthropicAPI: Forward Request (with updated tool definitions including $defs)
    AnthropicAPI-->>Gateway: Respond with Assistant Message (potentially with tool_use)

    Gateway->>Gateway: Call transformAssistantMessage(msg: Message)
    activate Gateway
    Gateway->>Gateway: Iterate msg.tool_calls
    loop For each toolCall
        Gateway->>Gateway: Access toolCall.function.arguments
        alt arguments is empty string or null/undefined
            Gateway->>Gateway: Set input = {}
        else arguments is non-empty string
            Gateway->>Gateway: Call JSON.parse(toolCall.function.arguments)
            Note right of Gateway: Handles potential malformed JSON if not empty
        end
        Gateway->>Gateway: Construct AnthropicMessage with tool_use and parsed input
    end
    deactivate Gateway
    Gateway-->>User: Return Transformed Chat Completion Response
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

@VisargD VisargD merged commit 4215a94 into Portkey-AI:main Jun 11, 2025
2 checks passed
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.

2 participants