Skip to main content
This page shows how to validate and structure data when using Gravix Layer with PydanticAI for robust input/output validation and schema-driven workflows.

What You’ll Learn

  • How to validate LLM outputs with Pydantic schemas
  • How to use Gravix Layer’s API for structured data
  • Example: Enforcing output formats for data extraction

1. Install Required Packages

pip install pydantic-ai python-dotenv

2. Configure Your API Key

Add your API key to a .env file:
GRAVIXLAYER_API_KEY=your_api_key_here

3. Using PydanticAI with Gravix Layer

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider
import os
import json
from dotenv import load_dotenv

load_dotenv()
api_key = os.environ.get("GRAVIXLAYER_API_KEY")

# Create the model with OpenAIProvider
model = OpenAIChatModel(
    "meta-llama/llama-3.1-8b-instruct",
    provider=OpenAIProvider(
        base_url="https://api.gravixlayer.com/v1/inference",
        api_key=api_key
    )
)

# No structured output - just get plain text and parse it yourself
agent = Agent(model=model)

result = agent.run_sync(
    'Extract name and age from: John Doe, 34 years old. '
    'Return ONLY a JSON object with "name" and "age" fields, nothing else.'
)

# Try to parse as JSON
try:
    # Sometimes models wrap JSON in markdown code blocks
    output_text = result.output.strip()
    if output_text.startswith('```'):
        # Remove markdown code block markers
        output_text = output_text.split('```')[1]
        if output_text.startswith('json'):
            output_text = output_text[4:]
        output_text = output_text.strip()
    
    data = json.loads(output_text)
    print("Parsed data:", data)
    print(f"Name: {data['name']}, Age: {data['age']}")
except json.JSONDecodeError as e:
    print(f"Failed to parse JSON: {e}")
    print("You may need to adjust the prompt or parse the response differently")
Expected Output:
{'name': 'John Doe', 'age': 34}

Tips:
  • The model_settings argument must include the correct base_url and api_key for Gravix Layer.
PydanticAI lets you enforce structure and validation on LLM outputs. Gravix Layer provides the inference power for any schema-driven task.