# Pydantic AI

> Give a Pydantic AI agent Corfedo's code tools with CorfedoToolset, or use Corfedo AI as the agent's model with CorfedoModel.

Source: https://docs.corfedo.ai/connect/frameworks/pydantic-ai/

`corfedo.pydantic_ai` brings Corfedo to Pydantic AI. It gives your agent Corfedo's read tools as a toolset, or makes Corfedo AI the agent's model.

## Before you begin

- An API key file with **Search and read code**. Add **Ask Corfedo AI** to use `CorfedoModel`. See [API keys](https://docs.corfedo.ai/connect/authentication/#api-keys).
- Python 3.11 or later and the package with its extra:

```sh
pip install 'corfedo[pydantic-ai]'
```

## Give your agent Corfedo's tools

```python
import os
from pydantic_ai import Agent
from corfedo import CorfedoClient
from corfedo.pydantic_ai import CorfedoToolset

async with CorfedoClient.from_api_key_file(os.environ['CORFEDO_API_KEY_FILE']) as client:
    agent = Agent('openai:gpt-5', toolsets=[CorfedoToolset(client)])
    result = await agent.run('Where is the retry policy configured?')
    print(result.output)
```

The agent's own model runs the loop. Each tool returns Corfedo's result as JSON text. An invalid argument or an unknown or ambiguous repository becomes a retry, so the model can correct the call. `CorfedoToolset(client, groups=[], tools=['search_code', 'read_file'])` selects single tools.

**Check:** `result.all_messages()` contains calls to `search_code` or `read_file`, and the output cites files from your repositories.

## Use Corfedo AI as the model

```python
from pydantic_ai import Agent
from corfedo.pydantic_ai import CorfedoModel, CorfedoModelSettings

agent = Agent(
    CorfedoModel(client),
    model_settings=CorfedoModelSettings(corfedo_tools=['search_code']),
)
first = await agent.run('How are search requests routed?')
next_answer = await agent.run(
    'Which modules call that code?', message_history=first.all_messages()
)
print(next_answer.output)
```

The conversation lives in Corfedo. It continues from the `provider_details` kept in the message history, also after a JSON round trip, so earlier messages are not sent again.

| Setting | Effect |
| --- | --- |
| `corfedo_conversation_id` | Continues a conversation |
| `corfedo_sources`, `corfedo_references`, `corfedo_limits`, `corfedo_tools` | Repositories to read, context to start from, run limits and Corfedo's own tools |

The [profile chosen for the API](https://docs.corfedo.ai/connect/api/ai/profiles/) answers.

Settings merge across model, agent and run as Pydantic AI usually merges them. Function tools you add become client tools your agent runs. `agent.run_stream` shows progress. A structured `output_type` uses native JSON Schema output.

A response gives the conversation, the run, the outcome and the citations in `provider_details`, the run ID as `provider_response_id` and token usage in `RequestUsage`. Cancelling a run of the agent ends its subscription, not the Corfedo run. `client.ai.cancel` with the run ID stops it.

**Check:** the second answer refers to the first, which shows the conversation continued.

## Troubleshooting

| What you see | Why | What to do |
| --- | --- | --- |
| `CorfedoUnsupportedInputError` | The call has a system prompt, a sampling setting, non-text content, a native tool, or tool or prompted output mode | Remove it. The profile owns instructions and settings. |
| `CorfedoRunError` | The run ended without an answer | Read its outcome |
| `CorfedoOutputDiscardedError` | Text already streamed was replaced after the run recovered | Read the answer with `await client.ai.get_run(run_id)` |

## Related

- [Python SDK](https://docs.corfedo.ai/connect/sdks/python/) for the core client.
- [LangChain](https://docs.corfedo.ai/connect/frameworks/langchain/) for the same integration in LangChain.
