Repository navigation
fix(llms): fall back to non-strict schemas for dict-valued fields - #10263
aryansahai22 wants to merge 1 commit into
Conversation
generate_model_description() forced additionalProperties=false on every
object, which turned a dict/Mapping field into a closed object with no
properties. OpenAI and Anthropic strict mode then admit exactly one value
for it, {}, so a tool argument such as headers: dict[str, str] never
received any keys and a dict field on an output_pydantic/response_format
model always came back empty, with no error anywhere.
Strict mode cannot express such a field at all, so detect it with the new
allows_additional_properties() and send the schema non-strict with its
value schema intact: generate_model_description() reports the real strict
value, convert_tools_to_openai_schema() carries it instead of hard-coding
True, and the OpenAI and Anthropic tool converters check the schema before
applying their strict sanitizers. Tools and models without such a field
are sent strict and closed exactly as before.
Fixes crewAIInc#10262
Strix Security Review✅ No open security findings. PR summary: Fixes tool and structured-output schemas so a `dict`/`Mapping` field keeps its value schema and the schema is sent non-strict instead of being closed to an empty object, carrying the real `strict` flag through the OpenAI, Anthropic and LiteLLM conversion paths. Review summaryReviewed the schema-strictness change across the four production files it touches: the new Review 1 on this PR · Reviewed by Strix |
Related issue
Fixes #10262
Summary
generate_model_description()forcedadditionalProperties: falseon every object and labelled the resultstrict: True. Adict[str, X]/Mappingfield, which Pydantic emits as{"type": "object", "additionalProperties": <value schema>}, therefore became a closed object with no properties. Under OpenAI and Anthropic strict mode that schema admits exactly one value,{}, so a tool argument such asheaders: dict[str, str]never received any keys, and a dict field on anoutput_pydantic/response_formatmodel always came back empty. Nothing errored; the tool just ran with an empty dict.Strict mode cannot express such a field at all (both vendors require
additionalProperties: falseon every object), so the fix sends these schemas non-strict with the value schema intact instead of "fixing" them by deleting it:pydantic_schema_utils.allows_additional_properties()(new) reports whether any object in a schema allows additional properties. It walks the whole schema, including$defs,anyOfbranches and array items, with the same cycle guard the neighbouring helpers use.generate_model_description()computesstrictfrom the raw Pydantic schema, only closes objects when strict, and returns the real value.JsonSchemaInfo.strictbecomesbool. The OpenAI Responsestext.formatand Azureresponse_formatpaths already read this flag, so structured output with a dict field is now sent non-strict.convert_tools_to_openai_schema()carries that flag instead of hard-coding"strict": True, which fixes the Anthropic and LiteLLM paths that read it.Before and after, for the tool in the issue:
Verification
tests/utilities/test_pydantic_schema_utils.py:TestAllowsAdditionalProperties(value schema,true,Optional[dict]insideanyOf, dict in array items, dict behind a$ref, closed objects, explicitfalse, cyclic input).tests/utilities/test_converter.py:test_generate_model_description_dict_fieldnow asserts the value schema is kept andstrictisFalse(it previously only checked the flag wasTrue); newtest_generate_model_description_nested_dict_field.tests/utilities/test_agent_utils.py: aBaseToolwithheaders: dict[str, str]is converted non-strict with its schema intact; a tool without a dict stays strict and closed.tests/llms/openai/test_openai.pyandtests/llms/anthropic/test_anthropic.py: a dict-argument tool is sent non-strict with the schema intact while a sibling tool stays strict and sanitized.uv run ruff check lib/,uv run ruff format --check lib/,uv run mypy lib/cd lib/crewai && uv run pytest tests/utilities tests/llms tests/agents tests/tools(3181 passed, 35 skipped, 4 xfailed)Additional context
titlekeys), the same way the Responses API path already sends every tool. Gemini (parameters_json_schema) and Bedrock (inputSchema.json) pass the schema through, so they now receive the real dict schema instead of a closed empty object.model_config = ConfigDict(extra="allow")emitsadditionalProperties: trueand is now also sent non-strict, since strict mode cannot honour it either. No bundled tool declares that on itsargs_schema.openai_agent_tool_adapter.py) closes schemas on its own and is unchanged here.llm-generatedlabel (I cannot set labels on this repo).