Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
pydantic-ai-graph - simplify public generics (#539)
  • Loading branch information
dmontagu authored and samuelcolvin committed Jan 15, 2025
commit b03e7bc64107016e28456e270c95ea1c00819499
31 changes: 21 additions & 10 deletions examples/pydantic_ai_examples/email_extract_graph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations as _annotations

import asyncio
from dataclasses import dataclass
from datetime import datetime, timedelta

import logfire
Expand Down Expand Up @@ -48,7 +49,8 @@ def extract_system_prompt(ctx: RunContext[list[str]]):
return prompt


class ExtractEvent(BaseNode[State, None]):
@dataclass
class ExtractEvent(BaseNode[State]):
async def run(self, ctx: GraphContext[State]) -> CleanEvent:
event = await extract_agent.run(
ctx.state.email_content, deps=ctx.state.skip_events
Expand All @@ -73,7 +75,10 @@ def timestamp_system_prompt():
)


class CleanEvent(BaseNode[State, RawEventDetails]):
@dataclass
class CleanEvent(BaseNode[State]):
input_data: RawEventDetails

async def run(self, ctx: GraphContext[State]) -> InspectEvent:
start_ts, duration = await asyncio.gather(
timestamp_agent.run(self.input_data.start_ts),
Expand All @@ -89,7 +94,10 @@ async def run(self, ctx: GraphContext[State]) -> InspectEvent:
)


class InspectEvent(BaseNode[State, EventDetails, EventDetails | None]):
@dataclass
class InspectEvent(BaseNode[State, EventDetails | None]):
input_data: EventDetails

async def run(
self, ctx: GraphContext[State]
) -> ExtractEvent | End[EventDetails | None]:
Expand All @@ -104,15 +112,18 @@ async def run(
return End(None)
else:
ctx.state.skip_events.append(self.input_data.title)
return ExtractEvent(None)
return ExtractEvent()


graph = Graph[State, None, EventDetails | None](
ExtractEvent,
CleanEvent,
InspectEvent,
graph = Graph[State, EventDetails | None](
nodes=(
ExtractEvent,
CleanEvent,
InspectEvent,
)
)
print(graph.mermaid_code())
graph_runner = graph.get_runner(ExtractEvent)
print(graph_runner.mermaid_code())

email = """
Hi Samuel,
Expand All @@ -137,7 +148,7 @@ async def run(

async def main():
state = State(email_content=email)
result, history = await graph.run(None, state)
result, history = await graph_runner.run(state, None)
debug(result, history)


Expand Down
17 changes: 14 additions & 3 deletions pydantic_ai_graph/pydantic_ai_graph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
from .graph import Graph
from .graph import Graph, GraphRun, GraphRunner
from .nodes import BaseNode, End, GraphContext
from .state import AbstractState, Snapshot
from .state import AbstractState, EndEvent, Step, StepOrEnd

__all__ = 'BaseNode', 'End', 'GraphContext', 'Graph', 'Snapshot', 'AbstractState'
__all__ = (
'Graph',
'GraphRunner',
'GraphRun',
'BaseNode',
'End',
'GraphContext',
'AbstractState',
'EndEvent',
'StepOrEnd',
'Step',
)
14 changes: 14 additions & 0 deletions pydantic_ai_graph/pydantic_ai_graph/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ def type_arg_name(arg: Any) -> str:
return 'None'
else:
return arg.__name__


def get_parent_namespace(frame: types.FrameType | None) -> dict[str, Any] | None:
"""Attempt to get the namespace where the graph was defined.

If the graph is defined with generics `Graph[a, b]` then another frame is inserted, and we have to skip that
to get the correct namespace.
"""
if frame is not None:
if back := frame.f_back:
if back.f_code.co_filename.endswith('/typing.py'):
return get_parent_namespace(back)
else:
return back.f_locals
Loading