How to Fix 'output parsing error during development' in CrewAI (Python)
When CrewAI throws output parsing error during development, it usually means the agent returned text that does not match the structured output your task expected. In practice, this shows up when you ask for JSON, a Pydantic model, or a specific schema, but the LLM responds with extra prose, malformed JSON, or the wrong keys.
This is common during local development because prompts are still changing, tools are noisy, and the model is not always strict about format. The failure often bubbles up through crewai.tasks.task_output.TaskOutput or the task runner after CrewAI tries to parse the agent response.
The Most Common Cause
The #1 cause is a mismatch between what your task asks for and what the agent actually returns.
If you configure expected_output, output_json, or output_pydantic, CrewAI expects a structured response. If the model adds commentary like “Here’s the result:” or returns invalid JSON, parsing fails.
Broken vs fixed pattern
| Broken | Fixed |
|---|---|
| Agent returns free-form text | Agent returns strict JSON matching schema |
| Prompt says “return JSON” but no enforcement | Use output_json or output_pydantic with a clear schema |
| No validation of response shape | Validate fields before passing downstream |
# BROKEN
from crewai import Agent, Task, Crew
researcher = Agent(
role="Researcher",
goal="Summarize customer churn reasons",
backstory="You are precise."
)
task = Task(
description="Analyze churn reasons and return JSON.",
expected_output="A JSON object with keys: reasons, confidence",
agent=researcher
)
crew = Crew(agents=[researcher], tasks=[task])
result = crew.kickoff()
print(result)
# FIXED
from pydantic import BaseModel
from crewai import Agent, Task, Crew
class ChurnAnalysis(BaseModel):
reasons: list[str]
confidence: float
researcher = Agent(
role="Researcher",
goal="Summarize customer churn reasons",
backstory="You are precise."
)
task = Task(
description=(
"Analyze churn reasons. "
"Return ONLY valid JSON that matches this schema: "
'{"reasons": ["string"], "confidence": 0.0}'
),
expected_output="A valid ChurnAnalysis object",
output_pydantic=ChurnAnalysis,
agent=researcher
)
crew = Crew(agents=[researcher], tasks=[task])
result = crew.kickoff()
print(result)
If you are using TaskOutput, this is where the parse fails. The fix is not “try again harder”; it is to make the output contract explicit and narrow.
Other Possible Causes
1) The model adds markdown fences or extra prose
A lot of models return:
```json
{"reasons":["pricing","support"],"confidence":0.91}
CrewAI expects raw JSON in many structured-output paths. Strip markdown from the prompt and tell the agent not to wrap output in code fences.
```python
description = """
Return ONLY raw JSON.
Do not use markdown.
Do not include explanations.
"""
2) Your schema does not match the actual output shape
If your Pydantic model expects a list but the model returns a string, parsing fails.
from pydantic import BaseModel
class OutputSchema(BaseModel):
reasons: list[str] # expects list
# But model returns:
# {"reasons": "pricing"}
Fix either the schema or the prompt so they agree exactly.
3) Tool output polluted the final answer
When an agent uses tools, it may echo tool logs or partial results into its final response. That breaks parsers expecting clean structured output.
agent = Agent(
role="Analyst",
goal="Return structured findings only",
backstory="No commentary.",
verbose=True # useful for debugging, but can reveal noisy intermediate text
)
If tool traces are leaking into final output, tighten your prompt and reduce ambiguity around when to summarize versus when to answer.
4) You are using a weakly constrained prompt with temperature too high
High temperature makes formatting less reliable. For structured outputs, keep it low.
llm_config = {
"model": "gpt-4o-mini",
"temperature": 0.0
}
If you need deterministic parsing during development, do not run structured tasks at creative settings.
How to Debug It
- •
Print the raw task output before parsing
- •Check whether you are getting valid JSON or just natural language.
- •Look for markdown fences, trailing commas, or extra sentences.
- •
Turn on verbose logging
- •Set
verbose=Trueon agents and inspect intermediate steps. - •You want to see whether the failure comes from tool output or final generation.
- •Set
- •
Reduce the task to one field
- •Replace a complex schema with a minimal one.
- •Example: start with
{ "status": "ok" }, then add fields back one by one.
- •
Compare expected schema vs actual response
- •If using Pydantic, print
model.model_json_schema(). - •Check that every required key exists and every type matches exactly.
- •If using Pydantic, print
A practical pattern:
try:
result = crew.kickoff()
except Exception as e:
print(type(e).__name__)
print(str(e))
If you see messages like:
- •
OutputParserException - •
ValidationError - •
Could not parse LLM output - •
Failed to parse task output
then you know this is an output contract problem, not a network issue.
Prevention
- •Use
output_pydanticfor anything that downstream code depends on. - •Keep structured-output prompts strict: “Return only valid JSON. No markdown. No explanation.”
- •Run structured tasks with
temperature=0until the schema is stable. - •Add tests that validate parsed outputs before merging changes.
If you treat CrewAI outputs as contracts instead of suggestions, this error becomes easy to eliminate. Most teams hit it because they ask for structure but never actually enforce structure at the task boundary.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit