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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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

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.

1 participant