Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph Support #528

Merged
merged 57 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
68f5fe2
start pydantic-ai-graph
samuelcolvin Dec 22, 2024
615c5e2
lower case state machine
samuelcolvin Dec 22, 2024
9ffe8f0
starting tests
samuelcolvin Dec 22, 2024
dd60d15
add history and logfire
samuelcolvin Dec 22, 2024
73db282
add example, alter types
samuelcolvin Dec 22, 2024
03fcaf2
fix dependencies
samuelcolvin Dec 23, 2024
1bae9bf
fix ci deps
samuelcolvin Dec 23, 2024
61f3d2b
fix tests for other versions
samuelcolvin Dec 23, 2024
dc769c9
change node test times
samuelcolvin Dec 23, 2024
b03e7bc
pydantic-ai-graph - simplify public generics (#539)
dmontagu Jan 2, 2025
d0bdb87
Typo in Graph Documentation (#596)
izzyacademy Jan 3, 2025
16ccd03
fix linting
samuelcolvin Jan 7, 2025
bda6dfb
separate mermaid logic
samuelcolvin Jan 7, 2025
cce71e1
fix graph type checking
samuelcolvin Jan 7, 2025
2d9f9f3
bump
samuelcolvin Jan 7, 2025
7e98bf7
adding node highlighting to mermaid, testing locally
samuelcolvin Jan 7, 2025
743fa5a
bump
samuelcolvin Jan 7, 2025
f6aa929
fix type checking imports
samuelcolvin Jan 7, 2025
246755d
fix for python 3.9
samuelcolvin Jan 7, 2025
d985db4
simplify mermaid config
samuelcolvin Jan 8, 2025
c325789
remove GraphRunner
samuelcolvin Jan 8, 2025
c41d59a
add Interrupt
samuelcolvin Jan 9, 2025
0b19632
remove interrupt, replace with "next()"
samuelcolvin Jan 9, 2025
c1b8035
address comments
samuelcolvin Jan 9, 2025
7e24d9d
switch name to pydantic-graph
samuelcolvin Jan 10, 2025
b74d0e4
allow labeling edges and notes for docstrings
samuelcolvin Jan 10, 2025
7f34a0d
allow notes to be disabled
samuelcolvin Jan 10, 2025
15573e9
adding graph tests
samuelcolvin Jan 10, 2025
de6b9e7
more mermaid tests, fix 3.9
samuelcolvin Jan 10, 2025
08e87aa
rename node to start_node in graph.run()
samuelcolvin Jan 10, 2025
25d79aa
more tests for graphs
samuelcolvin Jan 10, 2025
6e62906
coverage in tests
samuelcolvin Jan 10, 2025
c9ebc49
cleanup graph properties
samuelcolvin Jan 10, 2025
997ba99
infer graph name
samuelcolvin Jan 11, 2025
3b22850
fix for 3.9
samuelcolvin Jan 11, 2025
452a62f
adding API docs
samuelcolvin Jan 11, 2025
707129f
fix state, more docs
samuelcolvin Jan 11, 2025
80d4713
fix graph api examples
samuelcolvin Jan 11, 2025
be1563d
starting graph documentation
samuelcolvin Jan 11, 2025
6fdd1e9
fix examples
samuelcolvin Jan 11, 2025
a882a6c
more graph documentation
samuelcolvin Jan 11, 2025
f2cd72a
add GenAI example
samuelcolvin Jan 11, 2025
111f2d0
more graph docs
samuelcolvin Jan 12, 2025
5d0a834
extending graph docs
samuelcolvin Jan 13, 2025
9a7ab8e
fix history serialization
samuelcolvin Jan 14, 2025
4cd9142
add history (de)serialization tests
samuelcolvin Jan 14, 2025
598fdd6
add mermaid diagram section to graph docs
samuelcolvin Jan 14, 2025
bf8b824
fix tests
samuelcolvin Jan 14, 2025
aec8fab
add exceptions docs
samuelcolvin Jan 14, 2025
7d4f31d
docs tweaks
samuelcolvin Jan 14, 2025
79cb2b3
copy edits from @dmontagu
samuelcolvin Jan 15, 2025
38a787e
fix pydantic-graph readme
samuelcolvin Jan 15, 2025
6db58d6
snapshot state after node execution, not before
samuelcolvin Jan 15, 2025
326e5f5
improve history (de)serialization
samuelcolvin Jan 15, 2025
5b8433b
fix for older python
samuelcolvin Jan 15, 2025
b4e44bf
Graph deps (#693)
samuelcolvin Jan 15, 2025
a2144d6
docs comments, GraphContext -> GraphRunContext
samuelcolvin Jan 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

env:
CI: true
COLUMNS: 120
COLUMNS: 150
UV_PYTHON: 3.12
UV_FROZEN: '1'

Expand Down Expand Up @@ -129,8 +129,8 @@ jobs:

- run: mkdir coverage

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

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ env*/
examples/pydantic_ai_examples/.chat_app_messages.sqlite
.cache/
.vscode/
/question_graph_history.json
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typecheck-pyright:

.PHONY: typecheck-mypy
typecheck-mypy:
uv run mypy --strict tests/typed_agent.py
uv run mypy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this cover the tests/ files? If so, was there a reason we didn't do this sooner?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no this just covers two specific files: tests/typed_agent.py and tests/typed_graph.py, the only change here is to put the config into pyproject.toml, not Makefile


.PHONY: typecheck
typecheck: typecheck-pyright ## Run static type checking
Expand Down
17 changes: 17 additions & 0 deletions docs/.hooks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@ def on_page_markdown(markdown: str, page: Page, config: Config, files: Files) ->
return markdown


# path to the main mkdocs material bundle file, found during `on_env`
bundle_path: Path | None = None


def on_env(env: Environment, config: Config, files: Files) -> Environment:
global bundle_path
for file in files:
if re.match('assets/javascripts/bundle.[a-z0-9]+.min.js', file.src_uri):
bundle_path = Path(file.dest_dir) / file.src_uri

env.globals['build_timestamp'] = str(int(time.time()))
return env


def on_post_build(config: Config) -> None:
"""Inject extra CSS into mermaid styles to avoid titles being the same color as the background in dark mode."""
if bundle_path.exists():
content = bundle_path.read_text()
content, _ = re.subn(r'}(\.statediagram)', '}.statediagramTitleText{fill:#888}\1', content, count=1)
bundle_path.write_text(content)


def replace_uv_python_run(markdown: str) -> str:
return re.sub(r'```bash\n(.*?)(python/uv[\- ]run|pip/uv[\- ]add|py-cli)(.+?)\n```', sub_run, markdown)

Expand Down
2 changes: 1 addition & 1 deletion docs/api/models/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Its primary use case is for more advanced unit testing than is possible with `Te

Here's a minimal example:

```py {title="function_model_usage.py" call_name="test_my_agent" lint="not-imports"}
```py {title="function_model_usage.py" call_name="test_my_agent" noqa="I001"}
from pydantic_ai import Agent
from pydantic_ai.messages import ModelMessage, ModelResponse
from pydantic_ai.models.function import FunctionModel, AgentInfo
Expand Down
2 changes: 1 addition & 1 deletion docs/api/models/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Utility model for quickly testing apps built with PydanticAI.

Here's a minimal example:

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

Expand Down
3 changes: 3 additions & 0 deletions docs/api/pydantic_graph/exceptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pydantic_graph.exceptions`

::: pydantic_graph.exceptions
3 changes: 3 additions & 0 deletions docs/api/pydantic_graph/graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pydantic_graph`

::: pydantic_graph.graph
3 changes: 3 additions & 0 deletions docs/api/pydantic_graph/mermaid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pydantic_graph.mermaid`

::: pydantic_graph.mermaid
12 changes: 12 additions & 0 deletions docs/api/pydantic_graph/nodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# `pydantic_graph.nodes`

::: pydantic_graph.nodes
options:
members:
- GraphRunContext
- BaseNode
- End
- Edge
- DepsT
- RunEndT
- NodeRunEndT
3 changes: 3 additions & 0 deletions docs/api/pydantic_graph/state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pydantic_graph.state`

::: pydantic_graph.state
37 changes: 37 additions & 0 deletions docs/examples/question-graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Question Graph

Example of a graph for asking and evaluating questions.

Demonstrates:

* [`pydantic_graph`](../graph.md)

## Running the Example

With [dependencies installed and environment variables set](./index.md#usage), run:

```bash
python/uv-run -m pydantic_ai_examples.question_graph
```

## Example Code

```python {title="question_graph.py"}
#! examples/pydantic_ai_examples/question_graph.py
```

The mermaid diagram generated in this example looks like this:

```mermaid
---
title: question_graph
---
stateDiagram-v2
[*] --> Ask
Ask --> Answer: ask the question
Answer --> Evaluate: answer the question
Evaluate --> Congratulate
Evaluate --> Castigate
Congratulate --> [*]: success
Castigate --> Ask: try again
```
Loading
Loading