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.
This commit is contained in:
Greyson LaLonde
2026-04-10 08:24:53 +08:00
parent 84b1b0a0b0
commit 7aac17f462
2 changed files with 10 additions and 1 deletions

View File

@@ -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

View File

@@ -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)