Skip to content

Commit c7daea5

Browse files
committed
fix: resolve CI test failures for Anthropic strict mode
- Change strict check from truthy to explicit True comparison - Update test expectations for Anthropic strict mode behavior - Add mock provider to test_anthropic_strict_mode_tool_mapping - Update inline snapshots for auto-strict mode on compatible schemas Fixes all 16 failing test jobs across Python 3.10-3.13
1 parent cb75151 commit c7daea5

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

pydantic_ai_slim/pydantic_ai/models/anthropic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,9 @@ def _map_tool_definition(self, f: ToolDefinition) -> BetaToolParam:
853853
'input_schema': f.parameters_json_schema,
854854
}
855855

856-
# ✅ CRITICAL: Use truthy check (same as OpenAI line 768)
857-
# NOT checking "is not None" (PR #3457's mistake)
858-
if f.strict:
856+
# ✅ CRITICAL: Only add strict if explicitly True (not None, not False)
857+
# This prevents the strict field from leaking into tools that don't explicitly set it
858+
if f.strict is True:
859859
tool_param['strict'] = True # type: ignore[typeddict-item]
860860

861861
return tool_param

tests/models/test_anthropic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ def my_tool(value: str) -> str: # pragma: no cover
545545
'required': ['value'],
546546
'type': 'object',
547547
},
548+
'strict': True,
548549
'cache_control': {'type': 'ephemeral', 'ttl': '5m'},
549550
}
550551
]
@@ -6542,7 +6543,8 @@ def test_anthropic_json_schema_transformer():
65426543
assert result['type'] == 'object'
65436544
assert result['additionalProperties'] is False
65446545
assert result['properties'] == schema['properties']
6545-
assert result['required'] == schema['required']
6546+
# In strict mode, Anthropic requires ALL properties to be in required list
6547+
assert result['required'] == ['name', 'age']
65466548
assert transformer.is_strict_compatible is True
65476549

65486550
# test auto-detect mode (strict=False)
@@ -6562,9 +6564,11 @@ def test_anthropic_strict_mode_tool_mapping():
65626564
Test AnthropicModel._map_tool_definition() supports strict mode.
65636565
"""
65646566
from pydantic_ai.models.anthropic import AnthropicModel
6567+
from pydantic_ai.providers.anthropic import AnthropicProvider
65656568
from pydantic_ai.tools import ToolDefinition
65666569

6567-
model = AnthropicModel('claude-sonnet-4-5')
6570+
# Use mock provider to avoid requiring real API key
6571+
model = AnthropicModel('claude-sonnet-4-5', provider=AnthropicProvider(api_key='test-key'))
65686572

65696573
tool_def_strict = ToolDefinition(
65706574
name='test_tool',

0 commit comments

Comments
 (0)