# PydanticAI

Source: https://aiand-docs-staging.pages.dev/ja/integrations/pydantic-ai/

[PydanticAI](https://ai.pydantic.dev/) は LLM を型付き関数として扱うフレームワークです。OpenAI 互換プロバイダ経由で ai& と接続できます。

## セットアップ

```bash
pip install pydantic-ai
```

```python
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

model = OpenAIChatModel(
    "google/gemma-4-31b-it",
    provider=OpenAIProvider(
        base_url="https://api.aiand.com/v1",
        api_key="sk-your-api-key",
    ),
)
```

## 型付きエージェント

```python
agent = Agent(model, system_prompt="Be concise.")
result = await agent.run("What is the capital of France?")
print(result.output)
```

## 構造化出力

```python
from pydantic import BaseModel

class Contact(BaseModel):
    name: str
    email: str

agent = Agent(model, output_type=Contact)
result = await agent.run("From: Jane Doe <jane@example.com>")
print(result.output.name, result.output.email)
```

スキーマは strict な `final_result` ツールのパラメータとして ai& に送信され、ツール呼び出しの引数が Pydantic モデルに復元されます。

## ツール

```python
agent = Agent(model)

@agent.tool_plain
def get_weather(city: str) -> str:
    """Get current weather for a city."""
    return f"Sunny in {city}, 22°C"

result = await agent.run("What's the weather in Tokyo?")
print(result.output)
```
