crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] reasoning=True create_reasoning_plan schema 400s on OpenAI strict models and the text fallback loops

Open TehilaTheStudent opened this issue 2 months ago • 5 comments

Description

Agent(reasoning=True) sends tool create_reasoning_plan with "strict": true, but tool_to_use is a union ("type": ["string", "null"]). OpenAI strict mode rejects that schema. reasoning_handler then falls back to text parsing, and the refinement loop does not finish: each plan mentions NOT READY, so the handler asks for another plan.

This is not #6204 (READY-string detection, closed) and not #3466 (ready parameter missing, closed). The function call never succeeds, so the text fallback is the path that loops. It is also not #7332 (two async tasks sharing one executor).

Steps to Reproduce

  1. Install crewai==1.15.21 and set OPENAI_API_KEY.
  2. Run:
from crewai import Agent, Crew, LLM, Process, Task

agent = Agent(
    role="Helper",
    goal="Answer the user.",
    backstory="You answer briefly.",
    llm=LLM(model="openai/o4-mini"),
    reasoning=True,
    verbose=True,
)
crew = Crew(
    agents=[agent],
    tasks=[
        Task(
            description="Say hello.",
            expected_output="A short greeting.",
            agent=agent,
        )
    ],
    process=Process.sequential,
    verbose=True,
)
crew.kickoff()
  1. Watch the OpenAI request for create_reasoning_plan.

Expected behavior

The reasoning tool call is accepted, a plan is returned, and the task runs. A nullable tool_to_use should be a strict-mode schema OpenAI accepts (anyOf of string and null), or the schema should go through the sanitizer in crewai/utilities/pydantic_schema_utils.py before "strict": true is set.

Screenshots/Code snippets

crewai/utilities/reasoning_handler.py (FUNCTION_SCHEMA):

"tool_to_use": {
    "type": ["string", "null"],
    "description": "Tool to use for this step, or null if no tool needed",
},

crewai/utilities/agent_utils.py then sends that parameters object with "strict": True.

crewai/utilities/pydantic_schema_utils.py already documents that OpenAI strict mode requires every schema to have a type key. This reasoning schema does not follow that.

Operating System

Other (specify in additional context)

Python Version

3.12

crewAI Version

1.15.21

crewAI Tools Version

not installed

Virtual Environment

Venv

Evidence

OpenAI POST /v1/chat/completions with model o4-mini returns:

Error code: 400 - {'error': {'message': "Invalid schema for function 'create_reasoning_plan': In context=('properties', 'steps', 'items', 'properties', 'tool_to_use'), schema must have a 'type' key.", 'type': 'invalid_request_error', 'param': None, 'code': None}}

Then:

WARNING [crewai.utilities.reasoning_handler] Error during function calling: ... Falling back to text parsing.

The text planner keeps answering with another plan whose steps contain the words NOT READY, and the handler prompts again: "You indicated you weren't ready. Refine your plan...". That loop held the agent executor for more than a minute on a one-word user message.

Possible Solution

Emit tool_to_use as anyOf: [{type: string}, {type: null}] (or sanitize FUNCTION_SCHEMA with the existing strict-mode helper) before setting "strict": true. After a 400, do not enter the text refinement loop.

Additional context

Reproduced in a Linux container (Python 3.12) against openai/o4-mini. The same schema is model-independent; any OpenAI model that enforces strict function schemas should 400. crewai-tools is not installed.

This report was written with AI assistance. Please add the llm-generated label if that is required.

TehilaTheStudent avatar Sep 23 '26 07:09 TehilaTheStudent

I can take this. I reproduced the invalid strict-schema shape on current main: the reasoning tool's nullable tool_to_use field uses a list-valued type, and the existing schema test currently pins that form. I will keep the fix focused on emitting an OpenAI-compatible anyOf with explicit string/null type branches and add a regression assertion for the strict schema, without changing the reasoning loop or provider-wide schema handling.

liang0417 avatar Sep 23 '26 08:09 liang0417

Implemented the focused strict-schema fix in #7727. The nullable ool_to_use field now uses explicit nyOf string/null branches, and the regression test requires that OpenAI-compatible shape. Local verification: 10 focused tests passed; Ruff and mypy passed.

liang0417 avatar Sep 23 '26 08:09 liang0417

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 Oct 23 '26 12:10 github-actions[bot]

Still reproduces on PyPI crewai 1.15.32 (2026-10-27). tool_to_use in reasoning_handler.py is still "type": ["string", "null"]. #7727 is the fix and was rebased onto main on 2026-10-23. Please keep this open.

@joaomdmoura

TehilaTheStudent avatar Oct 28 '26 05:10 TehilaTheStudent

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 28 '26 12:11 github-actions[bot]