Pydantic AI Slim

1.75.0 · active · verified Wed Apr 01

Pydantic AI Slim is an active Python agent framework that provides a slim package designed to use Pydantic with Large Language Models (LLMs), focusing on reduced dependencies. It enables developers to build type-safe LLM agents and handle structured outputs with minimal boilerplate, leveraging Pydantic's data validation capabilities. The library maintains a rapid release cadence, with version 1.75.0 currently available.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic Pydantic AI agent using a Google Gemini model. It initializes an `Agent` with a specified model and instructions, then runs a synchronous prompt to get a concise response. Ensure your `GOOGLE_API_KEY` environment variable is set for model access.

import os
from pydantic_ai import Agent

# Ensure GOOGLE_API_KEY is set in your environment
# Example: export GOOGLE_API_KEY="your-api-key-here"
api_key = os.environ.get('GOOGLE_API_KEY')
if not api_key:
    print("Warning: GOOGLE_API_KEY environment variable not set. The example may not run.")

agent = Agent(
    "google-gla:gemini-1.5-flash",
    instructions="You're a helpful assistant. Reply concisely in one sentence."
)

result = agent.run_sync("What is the capital of France?")
print(result.output)
# Expected output: 'The capital of France is Paris.'

view raw JSON →