|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Commands |
| 6 | + |
| 7 | +### Core Development Tasks |
| 8 | +- **Install dependencies**: `make install` (requires uv, pre-commit, and deno) |
| 9 | +- **Run all checks**: `make` (format, lint, typecheck, test with coverage) |
| 10 | +- **Format code**: `make format` |
| 11 | +- **Lint code**: `make lint` |
| 12 | +- **Type checking**: `make typecheck` (uses pyright) or `make typecheck-both` (pyright + mypy) |
| 13 | +- **Run tests**: `make test` (with coverage) or `make test-fast` (parallel, no coverage) |
| 14 | +- **Build docs**: `make docs` or `make docs-serve` (local development) |
| 15 | + |
| 16 | +### Single Test Commands |
| 17 | +- **Run specific test**: `uv run pytest tests/test_agent.py::test_function_name -v` |
| 18 | +- **Run test file**: `uv run pytest tests/test_agent.py -v` |
| 19 | +- **Run with debug**: `uv run pytest tests/test_agent.py -v -s` |
| 20 | + |
| 21 | +### Multi-Python Testing |
| 22 | +- **Install all Python versions**: `make install-all-python` |
| 23 | +- **Test all Python versions**: `make test-all-python` |
| 24 | + |
| 25 | +## Project Architecture |
| 26 | + |
| 27 | +### Core Components |
| 28 | + |
| 29 | +**Agent System (`pydantic_ai_slim/pydantic_ai/agent.py`)** |
| 30 | +- `Agent[AgentDepsT, OutputDataT]`: Main orchestrator class with generic types for dependency injection and output validation |
| 31 | +- Entry points: `run()`, `run_sync()`, `run_stream()` methods |
| 32 | +- Handles tool management, system prompts, and model interaction |
| 33 | + |
| 34 | +**Model Integration (`pydantic_ai_slim/pydantic_ai/models/`)** |
| 35 | +- Unified interface across providers: OpenAI, Anthropic, Google, Groq, Cohere, Mistral, Bedrock, HuggingFace |
| 36 | +- Model strings: `"openai:gpt-4o"`, `"anthropic:claude-3-5-sonnet"`, `"google:gemini-1.5-pro"` |
| 37 | +- `ModelRequestParameters` for configuration, `StreamedResponse` for streaming |
| 38 | + |
| 39 | +**Graph-based Execution (`pydantic_graph/` + `_agent_graph.py`)** |
| 40 | +- State machine execution through: `UserPromptNode` → `ModelRequestNode` → `CallToolsNode` |
| 41 | +- `GraphAgentState` maintains message history and usage tracking |
| 42 | +- `GraphRunContext` provides execution context |
| 43 | + |
| 44 | +**Tool System (`tools.py`, `toolsets/`)** |
| 45 | +- `@agent.tool` decorator for function registration |
| 46 | +- `RunContext[AgentDepsT]` provides dependency injection in tools |
| 47 | +- Support for sync/async functions with automatic schema generation |
| 48 | + |
| 49 | +**Output Handling** |
| 50 | +- `TextOutput`: Plain text responses |
| 51 | +- `ToolOutput`: Structured data via tool calls |
| 52 | +- `NativeOutput`: Provider-specific structured output |
| 53 | +- `PromptedOutput`: Prompt-based structured extraction |
| 54 | + |
| 55 | +### Key Design Patterns |
| 56 | + |
| 57 | +**Dependency Injection** |
| 58 | +```python |
| 59 | +@dataclass |
| 60 | +class MyDeps: |
| 61 | + database: DatabaseConn |
| 62 | + |
| 63 | +agent = Agent('openai:gpt-4o', deps_type=MyDeps) |
| 64 | + |
| 65 | +@agent.tool |
| 66 | +async def get_data(ctx: RunContext[MyDeps]) -> str: |
| 67 | + return await ctx.deps.database.fetch_data() |
| 68 | +``` |
| 69 | + |
| 70 | +**Type-Safe Agents** |
| 71 | +```python |
| 72 | +class OutputModel(BaseModel): |
| 73 | + result: str |
| 74 | + confidence: float |
| 75 | + |
| 76 | +agent: Agent[MyDeps, OutputModel] = Agent( |
| 77 | + 'openai:gpt-4o', |
| 78 | + deps_type=MyDeps, |
| 79 | + output_type=OutputModel |
| 80 | +) |
| 81 | +``` |
| 82 | + |
| 83 | +## Workspace Structure |
| 84 | + |
| 85 | +This is a uv workspace with multiple packages: |
| 86 | +- **`pydantic_ai_slim/`**: Core framework (minimal dependencies) |
| 87 | +- **`pydantic_evals/`**: Evaluation system |
| 88 | +- **`pydantic_graph/`**: Graph execution engine |
| 89 | +- **`examples/`**: Example applications |
| 90 | +- **`clai/`**: CLI tool |
| 91 | +- **`mcp-run-python/`**: MCP server implementation (Deno/TypeScript) |
| 92 | + |
| 93 | +## Testing Strategy |
| 94 | + |
| 95 | +- **Unit tests**: `tests/` directory with comprehensive model and component coverage |
| 96 | +- **VCR cassettes**: `tests/cassettes/` for recorded LLM API interactions |
| 97 | +- **Test models**: Use `TestModel` for deterministic testing |
| 98 | +- **Examples testing**: `tests/test_examples.py` validates all documentation examples |
| 99 | +- **Multi-version testing**: Python 3.9-3.13 support |
| 100 | + |
| 101 | +## Key Configuration Files |
| 102 | + |
| 103 | +- **`pyproject.toml`**: Main workspace configuration with dependency groups |
| 104 | +- **`pydantic_ai_slim/pyproject.toml`**: Core package with model optional dependencies |
| 105 | +- **`Makefile`**: Development task automation |
| 106 | +- **`uv.lock`**: Locked dependencies for reproducible builds |
| 107 | + |
| 108 | +## Important Implementation Notes |
| 109 | + |
| 110 | +- **Model Provider Integration**: Each provider in `models/` directory implements the `Model` abstract base class |
| 111 | +- **Message System**: Vendor-agnostic message format in `messages.py` with rich content type support |
| 112 | +- **Streaming Architecture**: Real-time response processing with validation during streaming |
| 113 | +- **Error Handling**: Specific exception types with retry mechanisms at multiple levels |
| 114 | +- **OpenTelemetry Integration**: Built-in observability support |
| 115 | + |
| 116 | +## Documentation Development |
| 117 | + |
| 118 | +- **Local docs**: `make docs-serve` (serves at http://localhost:8000) |
| 119 | +- **Docs source**: `docs/` directory (MkDocs with Material theme) |
| 120 | +- **API reference**: Auto-generated from docstrings using mkdocstrings |
| 121 | + |
| 122 | +## Dependencies Management |
| 123 | + |
| 124 | +- **Package manager**: uv (fast Python package manager) |
| 125 | +- **Lock file**: `uv.lock` (commit this file) |
| 126 | +- **Sync command**: `make sync` to update dependencies |
| 127 | +- **Optional extras**: Define groups in `pyproject.toml` optional-dependencies |
0 commit comments