Skip to main content

Company Data Extractor

This example demonstrates how to extract structured, schema-enforced JSON from unstructured text using llm_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

  1. Visit https://swarms.world/platform/api-keys
  2. Create an account or sign in
  3. Generate a new API key
  4. 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

Expected Output:
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” a record_sentiment function whose arguments are the structured result.
Expected Response: When the agent uses a tool, outputs[0].content is an array of tool call objects. The function.arguments field is a JSON string that matches your parameters schema:
The parsed 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.