New Tool Radar

AI Agents & Automation

How to Make a CrewAI Task Return JSON and Save It to a File

Set a CrewAI task to return structured JSON output and save it to a file so another step can read it.

Schematic with three steps: Define the output schema; Attach the schema and a file path; Check the file parses and the fields.

A CrewAI task can return structured output through a Pydantic model or a JSON schema, which keeps answers short and machine-checkable, and it can write that output to a file. That combination is what lets a later step, a Flow, or another program read the result without parsing free text. Define the schema first, attach it to the task, and check the file parses before anything else depends on it.

What you need

  • A CrewAI crew with a task already running
  • The field names the output must carry
  • A Pydantic model or JSON schema for those fields

Step 1: Define the output schema

Write a Pydantic model or JSON schema with the fields the next step needs, each with a short description. Keep it to the fields that matter, because the model must fill every one.

Step 2: Attach the schema and a file path to the task

Set the structured output on the task and give it a file path to write the result to. Run the crew once and open the file.

Step 3: Check the file parses and the fields are right

Load the file in a JSON parser and confirm every field is present with the right type and value. If a field is missing or mistyped, the schema or the task description needs tightening, not the file.

Worked example: synthetic JSON output

The table below is a synthetic example. It shows the expected result of structured output; no crew was run.

Field Expected In file?
title Short title Yes
score Number Yes
verdict pass or fail Yes
File parses as JSON Yes Pass

Expected result (UNTESTED). the task writes a file that parses as JSON, with every field present and correctly typed. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • A schema only fixes the shape, not the content; a wrong value that fits the schema still passes.
  • A direct language model call is more efficient than a crew when you only need one structured output.
  • Keep the schema short, because every extra field is another chance for the model to guess.

Sources

See all