EVEMISSTechnology

Repository 指南 · 概覽 · v6

pydantic / pydantic-ai

“How Python does AI. Agents, realtime voice, image generation, embeddings. Every model, every interface, typed end to end.” — 作者自述

  • AI Agents
  • Python
  • MIT · 開源授權
原始 repository
pydantic/pydantic-ai
來源平台
GitHub
擁有者 / 組織
pydantic
授權
MIT · 開源授權
分析的版本
a0022cd089c90121f101f7966fdb492a092d4330
最後驗證

原始專案有更新,本指南有部分內容可能已經過時。預設分支已前進到 f93bcff0c72c,2026-11-15 檢查。整份指南描述的是版本 a0022cd089c9。版本差異比對發現,本指南引用的 10 個程式碼區域在新版本有變動(列在下面);關於這些區域的敘述在新版本不一定成立,目前還沒有重新核對。

  • pydantic_ai_slim/pydantic_ai/agent/__init__.py:531-4856 · 行有變動
  • pydantic_ai_slim/pydantic_ai/messages.py:232-291 · 行有變動
  • pydantic_ai_slim/pydantic_ai/messages.py:575-621 · 行有變動
  • pydantic_ai_slim/pydantic_ai/messages.py:780-932 · 行有變動
  • pydantic_ai_slim/pydantic_ai/exceptions.py:172-187 · 行有變動
  • pydantic_ai_slim/pydantic_ai/models/__init__.py:747-2140 · 行有變動
  • …另有 4 個

本頁中文由負責的 AI 編輯依英文正式版本 v6 翻譯;程式碼名稱、路徑、行號與數字都經確定性檢查,與英文版一致。陳述與依據表保留英文原文,因為那是獨立驗證者核對過的紀錄。看英文原文

pydantic/pydantic-ai Repository 概覽說明

本頁用語說明
Entrypoint record(進入點記錄)
分析器標記為「程式可能從這裡開始執行」的檔案。附有 __main__ guard 摘錄時,表示檔案裡有 if __name__ == "__main__": 區塊,摘錄顯示它呼叫了什麼;沒有摘錄時,只是依檔名判斷。
Bounded static execution path(有界靜態執行路徑)
不執行程式、直接從原始碼重建出的呼叫鏈,走到固定步數就停。它說明在紙面上能追到多遠,不代表實際執行時會發生什麼。
Unresolved boundary(未解析邊界)
靜態路徑停下的地方:下一個呼叫進入外部函式庫,或不執行就無法確定。這是本次分析的邊界,不是 repository 的缺陷。
Static relations(靜態關係)
在原始碼中找到的呼叫與 import。「external or unresolved」表示指向分析範圍以外。
Module role(模組角色)
分析器依呼叫進出次數推論的檔案標籤(例如 core、entry/orchestration、leaf)。它描述的是在呼叫圖中的位置,不是作者的設計意圖。
Test-like files(類測試檔案)
名稱或位置看起來像測試的檔案。本次分析只計數,不執行。
Observed · inferred · author-claimed · unresolved
每句話的依據:直接讀自分析的檔案;由分析器從檔案推論;repository 作者自述(metadata、README);或本次分析無法確定。
Verified(已驗證)
本頁有兩種用法。在分析器的記錄裡(「verified provenance」「verified entrypoint」),它表示這筆記錄是直接從分析的檔案讀到的,也就是本指南所說的 observed;以進入點來說,就是檔案裡確實有 __main__ guard 的文字。分析器在 2026-10-04 修正後,只有 cli.py、main.py 這類像進入點的檔名、沒有 guard 的檔案,會記為 inferred(推測);在那之前分析的指南仍把這類檔案標成 verified,它依然只是對檔案用途的推測。它不代表程式被執行或測試過。「最後驗證」是這份指南最後一次通過本實驗室檢查的日期,檢查對象是所標示版本的分析記錄,不是對 repository 本身的審查。

一個多套件的 repository,主要是 Python,採用 MIT 授權。它包含位於 pydantic_ai_slim 的核心函式庫,另有 pydantic_graph、pydantic_evals、CLI 套件、範例和一個非常龐大的測試套件。靜態呼叫次數指向 messages、models 和 agent 模組是核心。大多數關係仍未解析,而且這份概覽沒有執行任何東西。

它是什麼

平台 metadata 把這個專案描述為一個有型別的 Python AI 工具組,涵蓋 agent、即時語音、影像生成和 embedding,橫跨各種模型與介面。這是作者的說法。

它的 GitHub topics 包括 agent-framework、genai、llm 和 pydantic。主要語言是 Python,授權是 MIT。以位元組計算,程式碼絕大多數是 Python,另有少量的 TypeScript、PLpgSQL、Shell 和 CSS。

在結構上,它是一個多套件的 repository:

  • 核心函式庫位於 pydantic_ai_slim/pydantic_ai,其中包含 Agent 類別;
  • pydantic_graph 和 pydantic_evals 各自帶有自己的 pyproject.toml;
  • clai 有另一份清單檔。

分析器自己的自動摘要把這個 repository 稱為一個 web 或 Node.js 應用程式,主要起點是一個 .claude 輔助腳本。這個標籤和平台 metadata 互相矛盾,這裡不依賴它。

它怎麼啟動

許多檔案帶有 __main__ 守衛,分散在 .github/scripts、examples、scripts 和 tests 裡。它們可以分成幾組:

  • 面向使用者的 CLI:pydantic_ai_slim/pydantic_ai/__main__.py(呼叫 cli_exit())、clai/clai/__main__.py 和 src/pydantic_clai2/pydantic_clai2/__main__.py;
  • examples/pydantic_ai_examples 裡可執行的範例,有些透過 uvicorn 提供 ASGI 應用程式,有些則使用 asyncio.run;
  • .github/scripts 和 scripts 裡的自動化與維護腳本,以及 tests 底下的 MCP 測試伺服器。

另外有些進入點只是依檔名推測。這些包括 slack_lead_qualifier/app.py、ui/_web/app.py 和 pydantic_clai2 的 cli/cli.py。

分析資料中唯一的有界靜態流程經過 .claude/skills/testing-skill/parse_cassette.py。它從 __main__ 走到 main,在 argparse.ArgumentParser 和 parser.parse_args 這類未解析的呼叫結束。這個檔案看起來是測試工具的輔助腳本,不是函式庫的請求路徑。CLI 呼叫如何到達 Agent 和模型,這次分析沒有確定。

結構與連結最多的模組

依檔案數量,最大的頂層目錄是 tests(3423)、src(787)、pydantic_ai_slim(490)、docs(454)、.github(255)和 examples(63)。分析器把 docs、src 和 tests 標為可能的子系統。

pydantic_ai_slim/pydantic_ai 內已解析的靜態呼叫次數暗示了以下角色:

  • messages.py(497 入/102 出)、exceptions.py(541 入/0 出)和 _utils.py(313 入)看起來是被廣泛共用的基礎元件。
  • agent/__init__.py(4 入/244 出)和 _agent_graph.py(29 入/157 出)看起來是呼叫其餘部分的協調層。
  • models/__init__.py(165 入/127 出)位在中間,可能是模型抽象的樞紐。
  • ui/vercel_ai/_adapter.py(0 入/138 出)和 realtime/_openai_protocol.py(84 入/39 出)看起來是面向整合的模組。

呼入次數最高的兩個模組是測試輔助模組:tests/clai2/menu_script.py(699)和 tests/clai2/dialog_script.py(558)。因此單看次數並不等於架構上的重要性。

依賴與測試

依賴記錄並不完整。分析資料顯示:

  • pydantic_graph 宣告了對 anyio、logfire-api、pydantic(>=2.12)和 typing-inspection 的 runtime 依賴。
  • pydantic_evals 把 logfire 列為 optional。
  • 根目錄的 pyproject.toml dev 群組包括 pytest、pytest-xdist、pytest-mock、inline-snapshot、dirty-equals、hypothesis、coverage 和 cassetter,以及 mypy、pyright 和 ruff。
  • src/pydantic_clai2/web-ui/package.json 宣告了一個使用 react、marked、highlight.js 和 dompurify 的 React/Vite 前端。
  • tests/ui_roundtrip/package.json 列出 @ag-ui/client、ai 和 rxjs。

這個 repository 有 10 個建置/依賴清單檔。解析器會讀 pyproject.toml、requirements 檔案和 package.json,但不讀 setup.py、setup.cfg、Pipfile 或 lock 檔。這裡沒有出現核心 pydantic_ai_slim 套件的依賴記錄,所以它的 runtime 需求沒有確定。

分析器統計了 3447 個測試或類測試檔案。連 .github/scripts 的自動化也有自己的測試,例如 test_agent_provider_health.py。

先讀什麼

  1. README.md 和根目錄的 pyproject.toml,兩者都被標為專案層級的重要檔案。
  2. 先看 pydantic_ai_slim/pydantic_ai/__main__.py 了解 CLI 進入點,再看 agent/__init__.py 裡的 Agent 類別。
  3. messages.py、models/__init__.py 和 _agent_graph.py,這些是高次數的核心候選。
  4. 一個小範例,例如 examples/pydantic_ai_examples/pydantic_model.py。它的 main 區塊呼叫 agent.run_sync,並印出結果的 output 和 usage。

分析器的重要檔案清單也把 .claude/skills/testing-skill/parse_cassette.py 和幾個 .github/scripts 檔案排得很高。它們被列入只是因為它們可能是進入點。它們看起來是 repository 的工具,可以晚點再看。

這次分析的限制

  • 在 150000 條抽取出的關係中,125235 條是外部或未解析的,24765 條是本地的。
  • 抽取上限是每個 repository 150000 條關係,而這次碰到了這個上限。掃描後段的檔案沒有邊,所以上面的次數數字並不完整。
  • 九個超過 400 kB 的檔案被列入清單但沒有被解析。這些包括 pydantic_ai_slim/pydantic_ai/models/openai.py 和 tests/test_agent.py。
  • 分析只是靜態的:動態 import、依賴注入、框架接線和動態分派都沒有解析。pydantic_clai2 包含一個 builtin_plugins 目錄;對其內容的任何動態載入都不會被靜態分析解析。
  • 安裝、建置和執行的推論並不完整。沒有檢查任何指令,這份概覽也沒有執行任何程式碼。

值得注意的符號

  • agent/__init__.py 裡的 Agent 是一個非常大的類別,橫跨第 531 到 4856 行。
  • messages.py 定義了提示與內容型別,例如 SystemPromptPart、ImageUrl、AudioUrl、VideoUrl、DocumentUrl 和 BinaryContent。
  • exceptions.py 定義了例外型別,包括 ModelRetry、ToolFailed、ApprovalRequired、CallDeferred、UserError、AgentRunError 和 RunCancelled。
  • models/__init__.py 定義了 Model 類別、StreamedResponse、ModelRequestParameters,以及 check_allow_model_requests / override_allow_model_requests 這一對。

這次分析無法確定的事

這些是撰寫模型對分析記錄自己的說明。lim_3、exec_* 或 claim_… 這類識別碼指的是那次分析裡的記錄;陳述與依據表引用的也是同一批記錄。

  • 分析器的用途摘要(「web or Node.js application」(web 或 Node.js 應用程式),主要起點在 .claude/skills/testing-skill/parse_cassette.py)和以 Python 為主的 metadata 互相矛盾,看起來是啟發式判斷的產物。
  • 有一則教學說法把 examples/pydantic_ai_examples/slack_lead_qualifier/app.py 列為起點,但那個檔案只是依檔名啟發式推測的進入點。
  • 核心 pydantic_ai_slim 套件的 runtime 依賴不在分析資料不完整的依賴清單裡。
  • 已達到關係上限(tests/clai2/test_web_keys.py 之後的檔案沒有邊),所以呼叫次數排名只是近似值。
  • pydantic_ai_slim/pydantic_ai/models/openai.py 因為過大而被略過,所以它的關係不在次數數字裡。
  • clai、pydantic_clai2 和 src/pydantic_ai_harness 套件和核心函式庫的關係,沒有在靜態上確定。
  • 分析器有一則教學說法表示沒有可用的有界執行路徑,但分析資料列出了 exec_* 路徑;確實存在的路徑只涵蓋一個工具腳本。
陳述與依據 — 38 條陳述,38 條經獨立驗證者確認(英文原文)

上面每一句都是一條陳述,各自引用分析裡的依據編號。編號是這次分析內部的;表格列出每條陳述靠的是什麼、依據有多強。

Every substantive statement above is a claim bound to grounding IDs of the analyzed revision. Global grounding IDs are namespaced by the analysis run.

Claim Epistemic status Verifier Grounding (global IDs)
c1 Platform metadata describes the project as a typed Python AI toolkit covering agents, realtime voice, image generation and embeddings across models and interfaces. author_claimed supported analysis_947322335585c32a:meta_description
c2 GitHub topics include agent-framework, genai, llm and pydantic; the primary language is Python and the license is MIT. observed supported analysis_947322335585c32a:meta_topics, analysis_947322335585c32a:meta_primary_language, analysis_947322335585c32a:meta_license
c3 Language byte counts are dominated by Python, with smaller amounts of TypeScript, PLpgSQL, Shell and CSS. observed supported analysis_947322335585c32a:meta_language_bytes
c4 The repository holds several packages: pydantic_ai_slim/pydantic_ai contains the Agent class; pydantic_graph and pydantic_evals have their own pyproject.toml; clai has its own manifest. observed supported analysis_947322335585c32a:py_class_517362e262dd, analysis_947322335585c32a:dep_2, analysis_947322335585c32a:dep_1, analysis_947322335585c32a:ev_manifest_1
c5 The analyzer's auto-summary labels the repository a web or Node.js application starting at a .claude helper script, which conflicts with the Python primary-language metadata. inferred supported analysis_947322335585c32a:summary_repository, analysis_947322335585c32a:meta_primary_language
c6 Many files contain a __main__ guard, including scripts under .github/scripts and scripts, examples under examples/pydantic_ai_examples, and MCP servers under tests. observed supported analysis_947322335585c32a:py_entry_74efdc9541b6, analysis_947322335585c32a:py_entry_e20e97f19c72, analysis_947322335585c32a:py_entry_711b9329c9af, analysis_947322335585c32a:py_entry_bea51f9f5f67
c7 CLI-style entrypoints exist: pydantic_ai_slim/pydantic_ai/__main__.py calls cli_exit(), clai/clai/__main__.py calls _cli.cli_exit('clai'), and src/pydantic_clai2/pydantic_clai2/__main__.py calls main(). observed supported analysis_947322335585c32a:py_entry_34c79207455c, analysis_947322335585c32a:py_entry_ced396f72e87, analysis_947322335585c32a:py_entry_af9725b4ce2e
c8 Some examples start ASGI apps with uvicorn (ag_ui, chat_app), while others such as weather_agent.py use asyncio.run. observed supported analysis_947322335585c32a:py_entry_effc9a1ba135, analysis_947322335585c32a:py_entry_a74ded2e260e, analysis_947322335585c32a:py_entry_38713d80fa6e
c9 slack_lead_qualifier/app.py, pydantic_ai/ui/_web/app.py and pydantic_clai2/cli/cli.py are flagged as entrypoints only by a filename heuristic. inferred supported analysis_947322335585c32a:entry_26, analysis_947322335585c32a:entry_34, analysis_947322335585c32a:entry_51
c10 The bounded static paths go from parse_cassette.py __main__ to main and end at unresolved boundaries such as argparse.ArgumentParser and parser.parse_args. This is test tooling, not the library request path. inferred supported analysis_947322335585c32a:exec_bb0400b95179, analysis_947322335585c32a:exec_06af0227393f
c11 How a CLI invocation reaches Agent and a model at runtime is not established by the packet. unresolved supported analysis_947322335585c32a:py_entry_34c79207455c, analysis_947322335585c32a:lim_1
c12 By file count the largest top-level directories are tests (3423), src (787), pydantic_ai_slim (490), docs (454), .github (255) and examples (63). inferred supported analysis_947322335585c32a:summary_repository
c13 The analyzer flags docs, src and tests as likely subsystems. observed supported analysis_947322335585c32a:subsys_1, analysis_947322335585c32a:subsys_2, analysis_947322335585c32a:subsys_3
c14 messages.py (497 in/102 out), exceptions.py (541 in/0 out) and _utils.py (313 in) have high incoming degree, suggesting widely shared building blocks. inferred supported analysis_947322335585c32a:role_215, analysis_947322335585c32a:role_195, analysis_947322335585c32a:role_110
c15 agent/__init__.py (4 in/244 out) and _agent_graph.py (29 in/157 out) are mostly callers, suggesting orchestration layers. inferred supported analysis_947322335585c32a:role_113, analysis_947322335585c32a:role_66
c16 models/__init__.py has 165 incoming and 127 outgoing calls, suggesting it acts as a central model abstraction hub. inferred supported analysis_947322335585c32a:role_216
c17 ui/vercel_ai/_adapter.py (0 in/138 out) is an orchestration candidate and realtime/_openai_protocol.py (84 in/39 out) a service candidate; both look integration-facing. inferred supported analysis_947322335585c32a:role_355, analysis_947322335585c32a:role_305
c18 The highest-incoming modules are test helpers tests/clai2/menu_script.py (699) and tests/clai2/dialog_script.py (558), so call degree alone does not indicate architectural importance. inferred supported analysis_947322335585c32a:role_451, analysis_947322335585c32a:role_450
c19 The Agent class in pydantic_ai_slim/pydantic_ai/agent/__init__.py spans lines 531 to 4856. observed supported analysis_947322335585c32a:py_class_517362e262dd
c20 messages.py defines SystemPromptPart, ImageUrl, AudioUrl, VideoUrl, DocumentUrl and BinaryContent classes. observed supported analysis_947322335585c32a:py_class_f03b7148046e, analysis_947322335585c32a:py_class_88d890e791ae, analysis_947322335585c32a:py_class_53d036101117, analysis_947322335585c32a:py_class_79818bbe4915, analysis_947322335585c32a:py_class_7d9ee00f2022, analysis_947322335585c32a:py_class_e35c8c774bfa
c21 exceptions.py defines ModelRetry, ToolFailed, ApprovalRequired, CallDeferred, UserError, AgentRunError and RunCancelled classes. observed supported analysis_947322335585c32a:py_class_dd1ef28e575a, analysis_947322335585c32a:py_class_d525faa46c8c, analysis_947322335585c32a:py_class_4cb32d30d919, analysis_947322335585c32a:py_class_4cba1244d383, analysis_947322335585c32a:py_class_1a7aa408702e, analysis_947322335585c32a:py_class_a9d76143391e, analysis_947322335585c32a:py_class_b369b4dafdf8
c22 models/__init__.py defines Model, StreamedResponse, ModelRequestParameters, check_allow_model_requests and override_allow_model_requests. observed supported analysis_947322335585c32a:py_class_5cc493c3d87b, analysis_947322335585c32a:py_class_4407e19a57c9, analysis_947322335585c32a:py_class_43dbde63f4b6, analysis_947322335585c32a:py_func_f45b4390fe45, analysis_947322335585c32a:py_func_1f0af8e6b010
c23 pydantic_graph declares runtime dependencies anyio, logfire-api, pydantic (>=2.12) and typing-inspection; pydantic_evals lists logfire as optional. observed supported analysis_947322335585c32a:dep_2, analysis_947322335585c32a:dep_3, analysis_947322335585c32a:dep_4, analysis_947322335585c32a:dep_5, analysis_947322335585c32a:dep_1
c24 The root pyproject.toml dev group includes pytest, pytest-xdist, pytest-mock, inline-snapshot, dirty-equals, hypothesis, coverage, cassetter, mypy, pyright and ruff. observed supported analysis_947322335585c32a:dep_19, analysis_947322335585c32a:dep_25, analysis_947322335585c32a:dep_22, analysis_947322335585c32a:dep_18, analysis_947322335585c32a:dep_12, analysis_947322335585c32a:dep_41, analysis_947322335585c32a:dep_11, analysis_947322335585c32a:dep_40, analysis_947322335585c32a:dep_43, analysis_947322335585c32a:dep_44, analysis_947322335585c32a:dep_45
c25 src/pydantic_clai2/web-ui/package.json declares react, marked, highlight.js, dompurify and vite; tests/ui_roundtrip/package.json lists @ag-ui/client, ai and rxjs. observed supported analysis_947322335585c32a:dep_50, analysis_947322335585c32a:dep_49, analysis_947322335585c32a:dep_48, analysis_947322335585c32a:dep_47, analysis_947322335585c32a:dep_56, analysis_947322335585c32a:dep_57, analysis_947322335585c32a:dep_58, analysis_947322335585c32a:dep_59
c26 The repository has 10 build/dependency manifests; dependency parsing covers requirements files, package.json and pyproject.toml but not setup.py, setup.cfg, Pipfile or lock files. observed supported analysis_947322335585c32a:ev_manifest_1, analysis_947322335585c32a:lim_3a
c27 No dependency records for the core pydantic_ai_slim package appear in the packet, so its runtime requirements are not established. unresolved supported analysis_947322335585c32a:lim_3a, analysis_947322335585c32a:ev_manifest_1
c28 The analyzer counts 3447 test or test-like files, including tests for .github/scripts automation such as test_agent_provider_health.py. observed supported analysis_947322335585c32a:ev_tests_1
c29 README.md and pyproject.toml are flagged as project-level important files. observed supported analysis_947322335585c32a:important_1, analysis_947322335585c32a:important_2
c30 pydantic_ai_slim/pydantic_ai/__main__.py is a CLI entry with a __main__ guard, and agent/__init__.py holds the Agent class. observed supported analysis_947322335585c32a:py_entry_34c79207455c, analysis_947322335585c32a:py_class_517362e262dd
c31 messages.py, models/__init__.py and _agent_graph.py are high-degree service/core candidates and a reasonable next read. inferred supported analysis_947322335585c32a:role_215, analysis_947322335585c32a:role_216, analysis_947322335585c32a:role_66
c32 examples/pydantic_ai_examples/pydantic_model.py has a main block that calls agent.run_sync and prints result.output and result.usage. observed supported analysis_947322335585c32a:py_entry_e76ff878e091
c33 parse_cassette.py and .github/scripts files rank as important only because they are likely entrypoints; they appear to be repository tooling rather than library API. inferred supported analysis_947322335585c32a:important_4, analysis_947322335585c32a:important_5, analysis_947322335585c32a:important_8
c34 Of 150000 static relations, 125235 are external or unresolved and 24765 are local. observed supported analysis_947322335585c32a:relation_counts
c35 Relation extraction is capped at 150000 per repository and the total equals that cap, so later files lack edges and the reported degrees are incomplete. inferred supported analysis_947322335585c32a:lim_6, analysis_947322335585c32a:relation_counts
c36 Nine files over 400 kB were inventoried but not parsed, including pydantic_ai_slim/pydantic_ai/models/openai.py and tests/test_agent.py. inferred supported analysis_947322335585c32a:claim_cc68ccd3be32, analysis_947322335585c32a:lim_6
c37 The analysis is static only and does not resolve dynamic imports, dependency injection, framework wiring or dynamic dispatch. pydantic_clai2 contains a builtin_plugins directory; whether its contents are loaded dynamically is not established. inferred supported analysis_947322335585c32a:lim_1, analysis_947322335585c32a:entry_49
c38 Install, build and run inference is partial and installation commands are not verified; no code was executed for this overview. observed supported analysis_947322335585c32a:lim_2, analysis_947322335585c32a:lim_1

來源、權利與說明 · attribution-license-templates/v0.1

權利聲明。原始 repository 託管於 GitHub。其原始碼、文件、名稱、媒體與相關素材的權利,仍屬各自的作者、貢獻者與其他權利人所有,並受該 repository 的授權條款約束。

Original repository hosted on GitHub. Repository source code, documentation, names, media, and related project materials remain subject to the rights of their respective authors, contributors, and other rights holders and to applicable repository license terms.

平台聲明。GitHub 是所連結 repository 的來源託管平台。GitHub 與相關標誌為 GitHub, Inc. 的商標。除非另有明確說明,EVEMISS Technology 與 GitHub 之間沒有隸屬或背書關係。

GitHub is the source hosting platform for the linked repository. GitHub and related marks are trademarks of GitHub, Inc. EVEMISS Technology is not affiliated with or endorsed by GitHub unless explicitly stated otherwise.

這一頁怎麼來的。本頁由對應版本的 repository 分析與 AI 輔助的編輯工具產生。技術陳述綁定所分析的版本;原始 repository 更新後,可能重新驗證。

This page was produced using revision-aware repository analysis and AI-assisted editorial tooling. Technical claims are tied to the analyzed repository revision and may be revalidated when the source repository changes.

本頁包含 AI 輔助分析,並經 EVEMISS Technology 人工與 AI 協力審閱。 · 回報權利疑慮 · 所有 repository 指南