agent_docs/api-design.md
Rules for designing public APIs, managing visibility, backward compatibility, and API patterns
When to check: When designing or modifying public APIs, parameters, or class interfaces
_) and exclude from __all__ — prevents accidental API surface expansion and signals internal-only usage — Keeps the public API surface minimal and clearly separates internal implementation from stable public interfaces, preventing backward compatibility obligations for internal code.pydantic_ai package — hides internal structure and simplifies user imports — Makes the public API easier to use and allows internal refactoring without breaking user code that would otherwise depend on specific submodule paths_: KW_ONLY marker before optional fields in dataclasses/Pydantic models — Prevents breakage when adding parameters — callers can't accidentally pass defaults positionally, ensuring backward compatibility when fields are added or reorderedself attributes or enabling polymorphism; use module-level functions when no instance state is needed — Reduces unnecessary coupling and parameter passing while enabling proper polymorphism; extract shared logic to private top-level helpers to avoid duplication across classesupdate_*/*_inplace) — Immutable transforms prevent surprising side effects and make code easier to reason about, improving maintainability across the codebase_prefixed) — use public APIs, properties, or constructor parameters — Prevents breakage when internal implementation changes and ensures compatibility with library updatesModelSettings, embedding settings) when 2-3+ providers support them; maintain backward compatibility with automatic mapping from new common fields to legacy provider-prefixed fields — Prevents API duplication across provider-specific subclasses (e.g., OpenAIEmbeddingSettings, CohereEmbeddingSettings) while preserving backward compatibility when refactoring provider-prefixed parameters (e.g., cohere_, openai_) to shared fieldsBuiltinToolReturnPart.content flat and non-redundant — avoid duplicating part fields, repeating return_value data, single-key wrappers, or unnecessary lists — Reduces API surface area, prevents inconsistencies between duplicate fields, and simplifies consumption for both users and AI assistants'provider:model' format (e.g., 'openai:gpt-4', 'anthropic:claude-3') and infer_model() for instantiation — Ensures consistent model reference syntax across code, docs, and CLI; provides unified instantiation interface that prevents fragmentation