Skip to main content

Gravix Layer x PydanticAI Integration

Use Gravix Layer's LLMs with PydanticAI for structured output validation and schema-driven AI applications.

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, StructuredDict
import os
from dotenv import load_dotenv

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

agent = Agent(
model="meta-llama/llama-3.1-8b-instruct",
output_type=StructuredDict({
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
}),
model_settings={
"base_url": "https://api.gravixlayer.com/v1/inference",
"api_key": api_key
}
)

result = agent.run_sync("Extract name and age from: John Doe, 34 years old.")
print(result.output)

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.