crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Agent planning drops the JSON output requirements in Task.expected_output

Open master1018 opened this issue 4 weeks ago • 1 comments

Description

With agent planning enabled, my billing agent calculates the correct total but returns ordinary text instead of the JSON receipt requested in Task.expected_output. The required receipt_id is missing. The same task without planning returns the expected JSON.

This happens when I configure an agent to produce JSON receipts and use short planning steps for the application's status panel. I reduced the example to adding the price of a sandwich and a coffee: $8 + $3.

Steps to Reproduce

first, config api key for llm model, here I use deepseek-v4.1-flash

second, run the following code


import os

from crewai import Agent, Crew, LLM, PlanningConfig, Task

llm = LLM(
    model="openai/deepseek-flash",
    api_key=os.environ["DEEPSEEK_API_KEY"],
    base_url="https://api.deepseek.com/v1",
    custom_openai=True,
    reasoning_effort="max",
)

for planning in [False, True]:
    print(f"\n=== planning={planning} ===", flush=True)
    agent = Agent(
        role="Billing assistant",
        goal="Calculate purchase totals",
        backstory="You prepare receipts for small purchases.",
        llm=llm,
        verbose=True,
        planning_config=PlanningConfig(
            reasoning_effort="low",
            max_steps=1,
            system_prompt="Create a brief action plan for a compact status panel. "
                          "Use at most eight words per step. Do not write the answer in the plan.",
        ) if planning else None,
    )
    task = Task(
        description="Calculate the total for a sandwich costing $8 and a coffee costing $3.",
        expected_output='Return only one JSON object with total, currency="USD", '
                        'and receipt_id="ORDER-1042".',
        agent=agent,
    )
    result = Crew(agents=[agent], tasks=[task], verbose=True, tracing=False).kickoff()
    print("FINAL:", result.raw, flush=True)

Expected behavior

Both runs should return a JSON receipt:

{"total": 11, "currency": "USD", "receipt_id": "ORDER-1042"}

Screenshots/Code snippets

run the code in Steps to Reproduce, and you will see the following (without plan, and with plan)

Image Image

Operating System

Ubuntu 22.04

Python Version

3.11

crewAI Version

1.15.35

crewAI Tools Version

1.15.35

Virtual Environment

Conda

Evidence

Image Image

Possible Solution

None

Additional context

No additional context

master1018 avatar Nov 04 '26 08:11 master1018

I'd like to work on this.

Initial trace on main: in the Plan-and-Execute path, CrewAgentExecutor.finalize() takes the final answer either straight from the last completed todo (_can_use_last_todo_result_as_final_answer) or from _synthesize_final_answer_from_todos(). Neither path carries the task's expected_output: the shortcut returns the last step's raw free-text result, and the synthesis prompt is built from task.description only. So a natural-language format requirement like "Return only one JSON object..." reaches the per-step context but is dropped when the final answer is produced. Without planning, expected_output is part of the task prompt the agent sees throughout, which is why the same task returns JSON there.

I plan to thread the task's expected_output into finalization (synthesis prompt, and shortcut handling for format-sensitive outputs) and add a regression test with a fake LLM covering the planning-on vs planning-off JSON case from this report.

adarsh6980 avatar Nov 04 '26 09:11 adarsh6980