Back to blog

How to Build a Multimodal AI Agent That Returns JSON

Jake McCluskey
How to Build a Multimodal AI Agent That Returns JSON

You build an AI agent that processes images and returns structured data by using a multimodal vision model that can reason across both images and text prompts simultaneously, then constraining its output format to return JSON instead of plain text descriptions. The key difference from basic image captioning is that you're creating an agent that takes context from your prompt, analyzes the image based on that specific context, and outputs typed, structured data you can use programmatically in your applications.

This isn't about wrapping a vision API with some parsing logic. It's about designing an agent that treats images as first-class inputs alongside your prompts and questions.

What Is the Difference Between an AI Caption Generator and a Multimodal Agent?

Traditional image captioning models generate descriptive text paragraphs about what they see in an image. They're single-purpose tools that output unstructured text you'd need to parse with additional code or regex patterns. A multimodal agent, by contrast, processes both the image and your specific question simultaneously to extract exactly what you need in a predefined format.

Caption generators tell you "This is a receipt from a coffee shop with items and prices." A multimodal agent returns a JSON object with vendor name, date, line items as arrays, subtotal, tax, and total as separate fields. That's the difference between descriptive text and actionable data.

The architecture differs fundamentally. Captioning models process images through a vision encoder, generate embeddings, then decode those into natural language. Multimodal agents fuse image embeddings with text prompt embeddings before the reasoning layer, allowing simultaneous cross-modal attention. This means the model considers your specific question while analyzing visual features, not after.

Real multimodal agents can handle diverse use cases with a single implementation. You don't build separate models for receipts, whiteboards, product photos, or documents. The same agent adapts based on your prompt and output schema, which saves roughly 70% of development time compared to building specialized models for each use case.

Why Structured JSON Output Matters for Production AI Applications

Structured outputs eliminate the brittle parsing layer that breaks production applications. When your vision model returns free-form text, you're stuck writing regex patterns, string splitting logic, or secondary LLM calls to extract structured fields. Each parsing step introduces failure points and latency.

JSON schemas enable direct integration into databases, APIs, and user interfaces. Your application receives typed fields with predictable structure, so you can immediately save data, trigger workflows, or update UI components. No intermediate processing required.

Type safety prevents downstream errors. When you define that "total" is a float and "items" is an array of objects, you get validation at the model output layer. Free-text parsing might interpret "$12.50" as a string, breaking calculations in your application logic.

The performance difference is measurable. Applications using structured outputs from multimodal agents process requests approximately 40% faster than those requiring text parsing steps, because you've eliminated the parsing layer entirely. If you're building customer-facing tools or handling high request volumes, that latency reduction matters.

Building a Multimodal AI Agent with Llama 4 Scout for Structured Output

Llama 4 Scout on Groq provides production-ready multimodal capabilities with 460 tokens per second processing speed. That's fast enough for real-time applications where users upload images and expect immediate structured responses. The model handles simultaneous image and text reasoning without requiring separate vision preprocessing steps.

Here's the practical architecture for building your multimodal agent. You'll need image encoding, prompt design with schema definition, output parsing, and validation.

Setting Up Image Processing and Encoding

Your agent needs images in base64 format. Most multimodal APIs accept base64-encoded images embedded directly in the request payload, which simplifies the architecture compared to hosting images separately and passing URLs.


import base64
from pathlib import Path

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

image_b64 = encode_image("receipt.jpg")

This encoding step runs client-side before your API call. Keep image file sizes under 5MB for optimal processing speed, though most receipt and document photos fall well below that threshold naturally.

Designing Prompts with Explicit Schema Definitions

Your prompt must specify exactly what structured output you expect. The clearer your schema definition, the more consistent your agent's responses. Don't rely on the model to infer structure from vague instructions.


prompt = """
Analyze this receipt image and extract the following information as valid JSON:

{
  "vendor_name": "string",
  "date": "YYYY-MM-DD format",
  "items": [
    {
      "name": "string",
      "quantity": number,
      "unit_price": number,
      "total": number
    }
  ],
  "subtotal": number,
  "tax": number,
  "total": number
}

Return ONLY valid JSON without any additional text or explanation.
"""

The instruction to return "ONLY valid JSON" is critical. Without it, models often add explanatory text before or after the JSON block, breaking your parsing logic. Honestly, this single line prevents about 80% of output parsing headaches.

Making the Multimodal API Call

With your image encoded and prompt defined, you construct the API request that sends both modalities together. The model processes them simultaneously, not sequentially.


from groq import Groq

client = Groq(api_key="your_api_key")

response = client.chat.completions.create(
    model="llama-4-scout",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": prompt
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{image_b64}"
                    }
                }
            ]
        }
    ],
    temperature=0.1,
    max_tokens=1024
)

structured_data = response.choices[0].message.content

Notice the low temperature setting of 0.1. For structured data extraction, you want deterministic outputs with minimal variation across similar images. Higher temperatures introduce inconsistency in field naming and value formatting.

Validating and Using the Structured Output

Even with explicit instructions, you should validate the JSON structure before using it in your application. Python's Pydantic library provides elegant validation with type checking.


import json
from pydantic import BaseModel, ValidationError
from typing import List
from datetime import date

class ReceiptItem(BaseModel):
    name: str
    quantity: float
    unit_price: float
    total: float

class ReceiptData(BaseModel):
    vendor_name: str
    date: date
    items: List[ReceiptItem]
    subtotal: float
    tax: float
    total: float

try:
    parsed_json = json.loads(structured_data)
    receipt = ReceiptData(**parsed_json)
    # Now you have validated, typed data to use
    print(f"Receipt from {receipt.vendor_name} totaling ${receipt.total}")
except (json.JSONDecodeError, ValidationError) as e:
    print(f"Output validation failed: {e}")

This validation layer catches malformed outputs before they reach your database or business logic. When the schema matches, you get type-safe objects you can confidently use throughout your application.

Implementing Multi-Use-Case Agents Without Hardcoded Logic

The real power of multimodal agents shows up when you handle multiple use cases with one implementation. Instead of building separate receipt scanners, whiteboard analyzers, and product photo extractors, you build one agent that adapts based on the prompt and schema you provide.

You create prompt templates for each use case, but the underlying agent architecture stays identical. For whiteboard analysis, you define a schema with "topics," "action_items," and "diagrams" fields. For product photos, you specify "product_name," "visible_features," "condition," and "estimated_category." Same agent, different schemas.

This approach scales efficiently. When you need to add a new use case, you write a new prompt template and define the output schema. No model retraining, no architectural changes, no separate API endpoints. Teams building multimodal applications report handling 10+ distinct use cases with a single agent implementation.

The conditional logic moves from your code into your prompts. Instead of writing "if image_type == 'receipt' then extract_receipt_fields()" you simply pass the appropriate prompt with schema. The agent handles the reasoning, and you handle the schema definition. Much cleaner separation of concerns.

If you're building broader AI systems with multiple agents working together, this structured output approach integrates naturally with parallel agent architectures that process multiple tasks simultaneously. Each agent can output its results in predictable formats that other agents consume without translation layers.

Llama 4 Scout Multimodal Agent Implementation Guide

Llama 4 Scout specifically excels at balancing speed with accuracy for structured extraction tasks. At 460 tokens per second on Groq's infrastructure, it processes typical receipt or document images with full structured output in under 2 seconds. Fast enough for interactive user experiences.

The model supports images up to 4096x4096 pixels, which covers virtually all smartphone photos and scanned documents. You don't need to implement complex image resizing or cropping logic before processing. The model handles various aspect ratios and resolutions without degradation in extraction accuracy.

For cost efficiency, Llama 4 Scout on Groq processes roughly 1000 image analysis requests for the same price as 100 requests through comparable proprietary APIs. If you're building a product that processes user-uploaded images at scale, this cost difference directly impacts your unit economics.

Look, when implementing production systems, consider adding retry logic for the small percentage of outputs that don't validate against your schema. A simple retry with a slightly modified prompt typically succeeds on the second attempt. This brings your success rate from approximately 94% on first attempt to over 99% with one retry.

Error handling matters more than perfect first-attempt rates. Build your agent to gracefully handle validation failures, log the problematic cases for analysis, and provide useful feedback to users when extraction fails. Real production systems handle edge cases better than they achieve perfect accuracy on clean examples.

If you're looking to develop these kinds of AI capabilities professionally, understanding the different layers of an AI agent system fits naturally into broader AI engineering skills that companies actively seek when hiring.

You now have a concrete path to building multimodal AI agents that return structured, typed data instead of requiring text parsing. The pattern works: encode images, design explicit schema prompts, validate outputs, handle errors gracefully. Start with one use case, validate the architecture works for your specific needs, then expand to additional use cases using the same agent foundation. The shift from building separate vision models to building adaptable multimodal agents fundamentally changes how quickly you can ship AI-powered features that process visual information.

Go deeper

Claude Tool Use Fundamentals: The Foundation of Every Agent

The core agent loop every framework wraps, taught directly against the Claude API. You'll build a working weather plus calculator agent, handle parallel tool calls, and cache tool definitions for cheaper multi-turn runs.

Read the white paper →
Ready to stop reading and start shipping?

Get a free AI-powered SEO audit of your site

We'll crawl your site, benchmark your local pack, and hand you a prioritized fix list in minutes. No call required.

Run my free audit