Company Data Extractor
This example demonstrates how to extract structured, schema-enforced JSON from unstructured text usingllm_args.response_format - perfect for data extraction, classification, and form processing. A second approach using tools_list_dictionary (OpenAI function-calling schemas) is covered below.
Step 1: Get Your Swarms API Key
- Visit https://swarms.world/platform/api-keys
- Create an account or sign in
- Generate a new API key
- Store it securely in your environment variables
Step 2: Setup
Step 3: Define Your Structured Output Agent
Create an agent with a JSON schema that defines the exact fields you want extracted:Step 4: Run the Extraction
The schema uses
"strict": true and "additionalProperties": false to guarantee the response matches your schema exactly. Every field in required will always be present in the output.Structured Outputs via tools_list_dictionary
A second way to get structured output is the tools_list_dictionary field on the agent config (Optional[List[Dict]] on AgentSpec). Instead of constraining the response format, you give the agent one or more OpenAI function-calling-style JSON schemas. The model responds by “calling” your function, and the arguments it passes are JSON that conforms to your parameters schema — effectively schema-enforced output.
Each entry in tools_list_dictionary follows the OpenAI function calling format:
Example: Sentiment Classifier
This agent classifies customer feedback by “calling” arecord_sentiment function whose arguments are the structured result.
outputs[0].content is an array of tool call objects. The function.arguments field is a JSON string that matches your parameters schema:
arguments give you the structured result:
function.arguments is a JSON string, not a parsed object — decode it with json.loads() (Python) or JSON.parse() (JavaScript) before use.Which approach should I use?
Both fields can be set on the same agent. For a deeper guide to tools and function calling, see Structured Outputs & Tools.