From 7aac17f46211c8549ae427aaca4393c6621d1784 Mon Sep 17 00:00:00 2001 From: Greyson LaLonde Date: Fri, 10 Apr 2026 08:24:53 +0800 Subject: [PATCH] fix: forward strict mode to Anthropic and Bedrock providers The OpenAI-format tool schema sets strict: true but this was dropped during conversion to Anthropic/Bedrock formats, so neither provider used constrained decoding. Without it, the model can return string "None" instead of JSON null for nullable fields, causing Pydantic validation failures. --- .../src/crewai/llms/providers/anthropic/completion.py | 4 ++++ lib/crewai/src/crewai/llms/providers/bedrock/completion.py | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py b/lib/crewai/src/crewai/llms/providers/anthropic/completion.py index b6df34b94..94afbb06d 100644 --- a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py +++ b/lib/crewai/src/crewai/llms/providers/anthropic/completion.py @@ -494,6 +494,10 @@ class AnthropicCompletion(BaseLLM): "required": [], } + func_info = tool.get("function", {}) + if func_info.get("strict"): + anthropic_tool["strict"] = True + anthropic_tools.append(anthropic_tool) return anthropic_tools diff --git a/lib/crewai/src/crewai/llms/providers/bedrock/completion.py b/lib/crewai/src/crewai/llms/providers/bedrock/completion.py index c25c9bfec..59c8ef28e 100644 --- a/lib/crewai/src/crewai/llms/providers/bedrock/completion.py +++ b/lib/crewai/src/crewai/llms/providers/bedrock/completion.py @@ -118,7 +118,7 @@ def _preprocess_structured_data( try: - from aiobotocore.session import ( # type: ignore[import-untyped] + from aiobotocore.session import ( get_session as get_aiobotocore_session, ) @@ -169,6 +169,7 @@ class ToolSpec(TypedDict, total=False): name: Required[str] description: Required[str] inputSchema: ToolInputSchema + strict: bool class ConverseToolTypeDef(TypedDict): @@ -1965,6 +1966,10 @@ class BedrockCompletion(BaseLLM): input_schema: ToolInputSchema = {"json": parameters} tool_spec["inputSchema"] = input_schema + func_info = tool.get("function", {}) + if func_info.get("strict"): + tool_spec["strict"] = True + converse_tool: ConverseToolTypeDef = {"toolSpec": tool_spec} converse_tools.append(converse_tool)