[BUG]: ensure_type_in_schemas incorrectly forces empty schemas {} to {"type": "object"} in anyOf/oneOf
Description
In lib/crewai/src/crewai/utilities/pydantic_schema_utils.py, ensure_type_in_schemas() converts empty schemas {} inside anyOf / oneOf into {"type": "object"}:
# lib/crewai/src/crewai/utilities/pydantic_schema_utils.py (lines 290-292)
for i, schema in enumerate(schema_list):
if isinstance(schema, dict) and schema == {}:
schema_list[i] = {"type": "object"}
In JSON Schema specification, {} represents an unconstrained schema (accepting any value, including primitives such as strings, numbers, and booleans). Converting {} to {"type": "object"} corrupts schema semantics:
- When a tool has a parameter typed as
AnyorOptional[Any], Pydantic generates{"anyOf": [{}, {"type": "null"}]}. ensure_type_in_schemastransforms this into{"anyOf": [{"type": "object"}, {"type": "null"}]}.- As a result, the tool parameter is restricted to dictionaries/objects only. If an LLM returns a valid scalar (e.g.
42or"ok"), validation fails or the LLM is forced to return{}instead of the requested scalar.
Steps to Reproduce
from copy import deepcopy
from crewai.utilities.pydantic_schema_utils import ensure_type_in_schemas
schema = {"anyOf": [{}, {"type": "null"}]}
result = ensure_type_in_schemas(deepcopy(schema))
print(result)
# Observed:
# {'anyOf': [{'type': 'object'}, {'type': 'null'}]}
Expected Behavior
Empty schemas {} (representing Any) should not be unconditionally converted to {"type": "object"}. If OpenAI strict mode requires an explicit type, converting {} to an object restricts valid scalar arguments. The conversion should either preserve {} when not enforcing strict object constraints or avoid coercing unconstrained types to object.
Additional Context
- Python environment: Python 3.10+ / Pydantic v2
- File:
lib/crewai/src/crewai/utilities/pydantic_schema_utils.py
Hi, I’m a student contributing through Stanford would like to take this issue. I can reproduce the behavior with the snippet above. Before opening the PR, could a maintainer confirm the intended behavior? If OpenAI strict mode needs an explicit type here, should {} stay unchanged, or should it be replaced with a broader type union? I’ll add tests for the Any and Optional[Any] cases.
Hi @sallyw02, thanks for verifying the reproduction! Feel free to take this issue and submit the PR.
To answer your question regarding schema design:
- According to the JSON Schema specification,
{}means "any value is valid" without constraint. Converting{}to{"type": "object"}restricts parameters typed asAnyorOptional[Any]from accepting primitive scalars (e.g. integers, strings, booleans). - The most spec-compliant and minimally invasive approach is keeping
{}unconstrained (i.e. skipping coercion whenschema == {}). - If OpenAI strict mode specifically complains about missing types in a context where strictness is enforced, an alternative would be expanding
{}into ananyOfunion of primitive types (string,number,integer,boolean,object,array). However, for general schema conversion, leaving{}untouched avoids corrupting valid types.
Looking forward to seeing your PR!
@chauvuusvn Thank you for the information - will start a PR
Retracting my previous comment. It was posted before the intended founder approval for external outreach; please disregard it. Apologies for the noise.
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.