> ## Documentation Index
> Fetch the complete documentation index at: https://openrouter.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# PydanticAI

> Using OpenRouter with PydanticAI

## Using PydanticAI

[PydanticAI](https://github.com/pydantic/pydantic-ai) provides a high-level interface for working with various LLM providers, including OpenRouter.

### Installation

```bash lines theme={null}
pip install 'pydantic-ai-slim[openai]'
```

### Configuration

You can use OpenRouter with PydanticAI through its OpenAI-compatible interface:

```python lines theme={null}
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel

model = OpenAIModel(
    "anthropic/claude-sonnet-4.6",  # or any other OpenRouter model
    base_url="https://openrouter.ai/api/v1",
    api_key="sk-or-...",
)

agent = Agent(model)
result = await agent.run("What is the meaning of life?")
print(result)
```

```typescript title="TypeScript" lines theme={null}
import { OpenRouter } from '@openrouter/sdk';
import { callModel } from '@openrouter/agent';

const client = new OpenRouter({ apiKey: process.env.OPENROUTER_API_KEY });

const result = callModel(client, {
  model: 'anthropic/claude-sonnet-4.6', // or any other OpenRouter model
  input: 'What is the meaning of life?',
});

console.log(await result.getText());

```

```go title="Go" lines theme={null}
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
)

func main() {
	body, _ := json.Marshal(map[string]any{
		"model": "anthropic/claude-sonnet-4.6", // or any other OpenRouter model
		"messages": []map[string]string{
			{"role": "user", "content": "What is the meaning of life?"},
		},
	})

	req, _ := http.NewRequest("POST", "https://openrouter.ai/api/v1/chat/completions", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("OPENROUTER_API_KEY"))
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	var result struct {
		Choices []struct {
			Message struct {
				Content string `json:"content"`
			} `json:"message"`
		} `json:"choices"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		log.Fatal(err)
	}
	if len(result.Choices) == 0 {
		log.Fatalf("no choices returned (status %s)", resp.Status)
	}
	fmt.Println(result.Choices[0].Message.Content)
}

```

```bash title="cURL" lines theme={null}
curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "anthropic/claude-sonnet-4.6",
    "messages": [
      {"role": "user", "content": "What is the meaning of life?"}
    ]
  }' | jq -r '.choices[0].message.content'

```

```text title="Output" theme={null}
The meaning of life is one of humanity's oldest and most debated questions. Here are some perspectives:

**Philosophical views:**
- **Existentialism** (Sartre, Camus): Life has no inherent meaning; we create our own
- **Aristotle**: Living virtuously and pursuing *eudaimonia* (flourishing/well-being)
- **Stoicism**: Living according to reason and nature

**Religious/Spiritual views:**
- Fulfilling a divine purpose
- Spiritual growth or enlightenment
- Love, service, and connection to something greater

**Scientific perspective:**
- Biologically, survival and reproduction
- Though many find this incomplete as a full answer

**Practical answers people commonly find meaningful:**
- Relationships and love
- Contributing to others
- Personal growth
- Creating something lasting
- Experiencing joy and beauty

**The honest answer** is that there's no single universally agreed-upon answer. This may actually be useful - it means you have genuine freedom to determine what makes life meaningful *to you*.

The question itself might matter as much as any answer - it pushes us to reflect on what we actually value.

What draws you to the question? I'm happy to explore any angle more deeply.
```

The output above is the answer text only. `print(result)` prints the whole run result object that wraps this text. To print just the answer, print the result's text attribute: `result.output` in current PydanticAI releases, `result.data` in older ones.

For more details about using PydanticAI with OpenRouter, see the [PydanticAI documentation](https://ai.pydantic.dev/models/#api_key-argument).
