Skip to content

fix: Add tool_calls array to OpenAI chat completions response#34

Merged
CaddyGlow merged 2 commits into
CaddyGlow:mainfrom
advenimus:fix/tool-calls-in-chat-completions
Jan 20, 2026
Merged

fix: Add tool_calls array to OpenAI chat completions response#34
CaddyGlow merged 2 commits into
CaddyGlow:mainfrom
advenimus:fix/tool-calls-in-chat-completions

Conversation

@advenimus
Copy link
Copy Markdown
Contributor

Summary

This PR fixes a bug where the convert__anthropic_message_to_openai_chat__response function was not converting Anthropic tool_use blocks to OpenAI tool_calls format in chat completion responses.

The Problem:
When Claude returns a tool use response, the converter correctly set finish_reason: "tool_calls" but the actual tool_calls array was missing from the response message:

{
  "choices": [{
    "finish_reason": "tool_calls",
    "message": {
      "role": "assistant",
      "content": ""
      // MISSING: "tool_calls": [...]
    }
  }]
}

The Fix:
This PR adds handling for tool_use blocks in the response converter, converting them to the OpenAI tool_calls format:

{
  "choices": [{
    "finish_reason": "tool_calls", 
    "message": {
      "role": "assistant",
      "content": null,
      "tool_calls": [{
        "id": "toolu_xxx",
        "type": "function",
        "function": {
          "name": "get_gmail",
          "arguments": "{}"
        }
      }]
    }
  }]
}

Use Case

This fix is required for n8n AI Agent workflows and other OpenAI-compatible clients that rely on the tool_calls array to execute function calls. Without this fix, the AI Agent sees the tools are available but Claude's tool call requests are lost in translation.

Changes

  • Added handling for tool_use blocks in convert__anthropic_message_to_openai_chat__response
  • Converts Anthropic tool_use format (id, name, input) to OpenAI tool_calls format (id, type, function.name, function.arguments)
  • Sets content to None when there's no text content (per OpenAI spec)
  • Added json import for serializing tool arguments

Test plan

  • Tested with CCProxy-API v0.2.0 on Docker
  • Verified tool_calls array is now present in responses
  • Tested with n8n AI Agent workflow - tools now execute correctly

The convert__anthropic_message_to_openai_chat__response function was
not converting Anthropic tool_use blocks to OpenAI tool_calls format.
This caused the finish_reason to be "tool_calls" but the actual
tool_calls array was missing from the response message.

This fix:
- Adds handling for tool_use blocks in the response converter
- Converts Anthropic tool_use format to OpenAI tool_calls format
- Includes the tool_calls array in the message when present

This is required for n8n AI Agent workflows and other OpenAI-compatible
clients that rely on the tool_calls array to execute function calls.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@CaddyGlow
Copy link
Copy Markdown
Owner

CaddyGlow commented Jan 20, 2026

Reused a shared helper to build OpenAI tool_calls for Anthropic adapters (streaming + non-streaming) to ensure consistent JSON serialization of function.arguments. Added unit coverage for tool_use -> tool_calls and strengthened the sample-based test on claude_messages_tools to assert tool names and JSON args.

Tests:

uv run pytest tests/unit/llms/formatters/test_anthropic_to_openai_helpers.py; 
uv run pytest tests/unit/llms/formatters/test_formatter_endpoint_samples.py -k anthropic_message_to_openai_chat.

@CaddyGlow CaddyGlow merged commit c05614a into CaddyGlow:main Jan 20, 2026
13 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