autogen icon indicating copy to clipboard operation
autogen copied to clipboard

AnthropicChatCompletionClient ignores the configured `tool_choice` and drops `strict` and `additionalProperties` from tools

Open Android-Login-Analysis opened this issue 2 weeks ago • 2 comments

What happened?

AnthropicChatCompletionClient changes three tool constraints before the request is sent, without any warning. We found this while testing how SDKs carry tool-call constraints to the provider.

  1. The typed config field tool_choice (AnthropicClientConfiguration, autogen_ext/models/anthropic/config/__init__.py:58-60) is stored but never used. create() sets tool_choice only from its own tool_choice parameter, which defaults to auto (_anthropic_client.py:660-664). AssistantAgent does not pass tool_choice to create(), so an agent built with AnthropicChatCompletionClient(tool_choice="none") (or {"type": "tool", ...}, or {"type": "auto", "disable_parallel_tool_use": True}) sends "tool_choice": {"type": "auto"}.
  2. convert_tools (_anthropic_client.py:319-358) copies only properties, required and type into input_schema. A top-level additionalProperties: false is lost.
  3. convert_tools never reads ToolSchema["strict"], so strict=True tools are sent without strict.

Repro (point base_url at a local server that logs request bodies):

from autogen_agentchat.agents import AssistantAgent
from autogen_core.models import ModelFamily
from autogen_ext.models.anthropic import AnthropicChatCompletionClient

def lookup_order(order_id: str) -> str:
    return "delivered"

def issue_refund(order_id: str, amount: int) -> str:
    return "refunded"

client = AnthropicChatCompletionClient(
    model="claude-sonnet-4-5", api_key="sk-dummy", base_url="http://127.0.0.1:8080",
    tool_choice="none",
    model_info={"vision": False, "function_calling": True, "json_output": False,
                "structured_output": False, "family": ModelFamily.CLAUDE_4_SONNET},
)
agent = AssistantAgent("assistant", model_client=client, tools=[lookup_order, issue_refund])
await agent.run(task="Check order ORD-123456")

Expected: "tool_choice": {"type": "none"} in the request (or the tool / disable_parallel_tool_use value set in config). Tool input_schema keeps additionalProperties: false, and strict: true tools carry "strict": true.

Actual: "tool_choice": {"type": "auto"}. input_schema has only type, properties, required. No strict. In our test the server then answered with tool calls, and the agent ran lookup_order under tool_choice: none, ran issue_refund when lookup_order was forced, and ran two calls when parallel calls were disabled.

Impact: an application that uses these settings to stop or limit tool calls gets auto behavior instead, and is not told.

Suggested fix: use the configured tool_choice when the create() parameter is not given, support disable_parallel_tool_use, and copy additionalProperties and strict in convert_tools.

Which packages was the bug in?

Python Extensions (autogen-ext)

AutoGen library version.

Python 0.7.5

Other library version.

anthropic 0.49.0

Model used

claude-sonnet-4-5

Model provider

Anthropic

Other model provider

No response

Python version

3.12

.NET version

None

Operating system

None

Android-Login-Analysis avatar Nov 11 '26 20:11 Android-Login-Analysis

Looking at python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py, there is a fourth schema-fidelity issue in convert_tools (lines 319-358) right next to additionalProperties and strict:

input_schema={
    "type": "object",
    "properties": tool.schema["parameters"]["properties"],
    "required": required,
}

When a tool parameter uses a nested Pydantic model, Enum, or recursive/shared sub-model (or when a tool comes from an MCP server via McpWorkbench), Pydantic v2 places the sub-schemas under tool.schema["parameters"]["$defs"] (or "definitions") and emits {"$ref": "#/$defs/SubModel"} inside properties. Because convert_tools only copies "type", "properties", and "required", any top-level "$defs", "definitions", and "additionalProperties" keys on parameters are stripped, leaving dangling "$ref" pointers inside input_schema["properties"].

Clean fix for all four in _anthropic_client.py

  1. In convert_tools (_anthropic_client.py:319-358): Preserve "additionalProperties", "$defs", and "definitions" from parameters (or inline $defs into properties), and forward strict onto ToolParam when tool.schema.get("strict") is set:
    params = tool.schema["parameters"]
    input_schema: dict[str, Any] = {
        "type": "object",
        "properties": params["properties"],
        "required": required,
    }
    for extra_key in ("additionalProperties", "$defs", "definitions"):
        if extra_key in params:
            input_schema[extra_key] = params[extra_key]
    
  2. In create() / create_stream() (_anthropic_client.py:660-664): Fall back to self._config.get("tool_choice") when the per-call tool_choice argument is left at its default and the caller configured tool_choice on AnthropicChatCompletionClient(...), and map "none" to omitting/disabling tool use ({"type": "none"} on current Anthropic Messages API) rather than forcing {"type": "auto"}.

(For MCP-backed tools where $defs/$ref stripping or verbose schemas cause similar validation and token issues across client SDKs, we also inline $defs/$ref directly at the stdio layer in SalyyS1/mcp-slim.)

SalyyS1 avatar Nov 11 '26 22:11 SalyyS1

What happened?

AnthropicChatCompletionClient changes three tool constraints before the request is sent, without any warning. We found this while testing how SDKs carry tool-call constraints to the provider.

  1. The typed config field tool_choice (AnthropicClientConfiguration, autogen_ext/models/anthropic/config/__init__.py:58-60) is stored but never used. create() sets tool_choice only from its own tool_choice parameter, which defaults to auto (_anthropic_client.py:660-664). AssistantAgent does not pass tool_choice to create(), so an agent built with AnthropicChatCompletionClient(tool_choice="none") (or {"type": "tool", ...}, or {"type": "auto", "disable_parallel_tool_use": True}) sends "tool_choice": {"type": "auto"}.
  2. convert_tools (_anthropic_client.py:319-358) copies only properties, required and type into input_schema. A top-level additionalProperties: false is lost.
  3. convert_tools never reads ToolSchema["strict"], so strict=True tools are sent without strict.

Repro (point base_url at a local server that logs request bodies):

from autogen_agentchat.agents import AssistantAgent
from autogen_core.models import ModelFamily
from autogen_ext.models.anthropic import AnthropicChatCompletionClient

def lookup_order(order_id: str) -> str:
    return "delivered"

def issue_refund(order_id: str, amount: int) -> str:
    return "refunded"

client = AnthropicChatCompletionClient(
    model="claude-sonnet-4-5", api_key="sk-dummy", base_url="http://127.0.0.1:8080",
    tool_choice="none",
    model_info={"vision": False, "function_calling": True, "json_output": False,
                "structured_output": False, "family": ModelFamily.CLAUDE_4_SONNET},
)
agent = AssistantAgent("assistant", model_client=client, tools=[lookup_order, issue_refund])
await agent.run(task="Check order ORD-123456")

Expected: "tool_choice": {"type": "none"} in the request (or the tool / disable_parallel_tool_use value set in config). Tool input_schema keeps additionalProperties: false, and strict: true tools carry "strict": true.

Actual: "tool_choice": {"type": "auto"}. input_schema has only type, properties, required. No strict. In our test the server then answered with tool calls, and the agent ran lookup_order under tool_choice: none, ran issue_refund when lookup_order was forced, and ran two calls when parallel calls were disabled.

Impact: an application that uses these settings to stop or limit tool calls gets auto behavior instead, and is not told.

Suggested fix: use the configured tool_choice when the create() parameter is not given, support disable_parallel_tool_use, and copy additionalProperties and strict in convert_tools.

Which packages was the bug in?

Python Extensions (autogen-ext)

AutoGen library version.

Python 0.7.5

Other library version.

anthropic 0.49.0

Model used

claude-sonnet-4-5

Model provider

Anthropic

Other model provider

No response

Python version

3.12

.NET version

None

Operating system

None

No#scammer#fake family or friend at all

behnamfarsi893-arch avatar Nov 12 '26 14:11 behnamfarsi893-arch