crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG]: ensure_type_in_schemas incorrectly forces empty schemas {} to {"type": "object"} in anyOf/oneOf

Open chauvuusvn opened this issue 1 month ago • 6 comments

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:

  1. When a tool has a parameter typed as Any or Optional[Any], Pydantic generates {"anyOf": [{}, {"type": "null"}]}.
  2. ensure_type_in_schemas transforms this into {"anyOf": [{"type": "object"}, {"type": "null"}]}.
  3. As a result, the tool parameter is restricted to dictionaries/objects only. If an LLM returns a valid scalar (e.g. 42 or "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

chauvuusvn avatar Oct 12 '26 04:10 chauvuusvn

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.

sallyw02 avatar Oct 12 '26 06:10 sallyw02

Hi @sallyw02, thanks for verifying the reproduction! Feel free to take this issue and submit the PR.

To answer your question regarding schema design:

  1. According to the JSON Schema specification, {} means "any value is valid" without constraint. Converting {} to {"type": "object"} restricts parameters typed as Any or Optional[Any] from accepting primitive scalars (e.g. integers, strings, booleans).
  2. The most spec-compliant and minimally invasive approach is keeping {} unconstrained (i.e. skipping coercion when schema == {}).
  3. If OpenAI strict mode specifically complains about missing types in a context where strictness is enforced, an alternative would be expanding {} into an anyOf union 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 avatar Oct 12 '26 07:10 chauvuusvn

@chauvuusvn Thank you for the information - will start a PR

sallyw02 avatar Oct 12 '26 17:10 sallyw02

Retracting my previous comment. It was posted before the intended founder approval for external outreach; please disregard it. Apologies for the noise.

hosamhosaam5-ops avatar Oct 13 '26 22:10 hosamhosaam5-ops

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.

github-actions[bot] avatar Nov 13 '26 12:11 github-actions[bot]