Skip to content

Commit 53fcb50

Browse files
samuelcolvindmontaguizzyacademy
authored
Graph Support (pydantic#528)
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com> Co-authored-by: Israel Ekpo <44282278+izzyacademy@users.noreply.github.com>
1 parent 7ed66ff commit 53fcb50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3512
-34
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010

1111
env:
1212
CI: true
13-
COLUMNS: 120
13+
COLUMNS: 150
1414
UV_PYTHON: 3.12
1515
UV_FROZEN: '1'
1616

@@ -129,8 +129,8 @@ jobs:
129129

130130
- run: mkdir coverage
131131

132-
# run tests with just `pydantic-ai-slim` dependencies
133-
- run: uv run --package pydantic-ai-slim coverage run -m pytest
132+
# run tests with just `pydantic-ai-slim` and `pydantic-graph` dependencies
133+
- run: uv run --package pydantic-ai-slim --extra graph coverage run -m pytest
134134
env:
135135
COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}-slim
136136

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ env*/
1313
examples/pydantic_ai_examples/.chat_app_messages.sqlite
1414
.cache/
1515
.vscode/
16+
/question_graph_history.json

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typecheck-pyright:
3434

3535
.PHONY: typecheck-mypy
3636
typecheck-mypy:
37-
uv run mypy --strict tests/typed_agent.py
37+
uv run mypy
3838

3939
.PHONY: typecheck
4040
typecheck: typecheck-pyright ## Run static type checking

docs/.hooks/main.py

+17
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,28 @@ def on_page_markdown(markdown: str, page: Page, config: Config, files: Files) ->
1919
return markdown
2020

2121

22+
# path to the main mkdocs material bundle file, found during `on_env`
23+
bundle_path: Path | None = None
24+
25+
2226
def on_env(env: Environment, config: Config, files: Files) -> Environment:
27+
global bundle_path
28+
for file in files:
29+
if re.match('assets/javascripts/bundle.[a-z0-9]+.min.js', file.src_uri):
30+
bundle_path = Path(file.dest_dir) / file.src_uri
31+
2332
env.globals['build_timestamp'] = str(int(time.time()))
2433
return env
2534

2635

36+
def on_post_build(config: Config) -> None:
37+
"""Inject extra CSS into mermaid styles to avoid titles being the same color as the background in dark mode."""
38+
if bundle_path.exists():
39+
content = bundle_path.read_text()
40+
content, _ = re.subn(r'}(\.statediagram)', '}.statediagramTitleText{fill:#888}\1', content, count=1)
41+
bundle_path.write_text(content)
42+
43+
2744
def replace_uv_python_run(markdown: str) -> str:
2845
return re.sub(r'```bash\n(.*?)(python/uv[\- ]run|pip/uv[\- ]add|py-cli)(.+?)\n```', sub_run, markdown)
2946

docs/api/models/function.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Its primary use case is for more advanced unit testing than is possible with `Te
99

1010
Here's a minimal example:
1111

12-
```py {title="function_model_usage.py" call_name="test_my_agent" lint="not-imports"}
12+
```py {title="function_model_usage.py" call_name="test_my_agent" noqa="I001"}
1313
from pydantic_ai import Agent
1414
from pydantic_ai.messages import ModelMessage, ModelResponse
1515
from pydantic_ai.models.function import FunctionModel, AgentInfo

docs/api/models/test.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Utility model for quickly testing apps built with PydanticAI.
44

55
Here's a minimal example:
66

7-
```py {title="test_model_usage.py" call_name="test_my_agent" lint="not-imports"}
7+
```py {title="test_model_usage.py" call_name="test_my_agent" noqa="I001"}
88
from pydantic_ai import Agent
99
from pydantic_ai.models.test import TestModel
1010

docs/api/pydantic_graph/exceptions.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `pydantic_graph.exceptions`
2+
3+
::: pydantic_graph.exceptions

docs/api/pydantic_graph/graph.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `pydantic_graph`
2+
3+
::: pydantic_graph.graph

docs/api/pydantic_graph/mermaid.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `pydantic_graph.mermaid`
2+
3+
::: pydantic_graph.mermaid

docs/api/pydantic_graph/nodes.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# `pydantic_graph.nodes`
2+
3+
::: pydantic_graph.nodes
4+
options:
5+
members:
6+
- GraphRunContext
7+
- BaseNode
8+
- End
9+
- Edge
10+
- DepsT
11+
- RunEndT
12+
- NodeRunEndT

docs/api/pydantic_graph/state.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `pydantic_graph.state`
2+
3+
::: pydantic_graph.state

docs/examples/question-graph.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Question Graph
2+
3+
Example of a graph for asking and evaluating questions.
4+
5+
Demonstrates:
6+
7+
* [`pydantic_graph`](../graph.md)
8+
9+
## Running the Example
10+
11+
With [dependencies installed and environment variables set](./index.md#usage), run:
12+
13+
```bash
14+
python/uv-run -m pydantic_ai_examples.question_graph
15+
```
16+
17+
## Example Code
18+
19+
```python {title="question_graph.py"}
20+
#! examples/pydantic_ai_examples/question_graph.py
21+
```
22+
23+
The mermaid diagram generated in this example looks like this:
24+
25+
```mermaid
26+
---
27+
title: question_graph
28+
---
29+
stateDiagram-v2
30+
[*] --> Ask
31+
Ask --> Answer: ask the question
32+
Answer --> Evaluate: answer the question
33+
Evaluate --> Congratulate
34+
Evaluate --> Castigate
35+
Congratulate --> [*]: success
36+
Castigate --> Ask: try again
37+
```

0 commit comments

Comments
 (0)