diff --git a/pydantic_ai_slim/pydantic_ai/agent.py b/pydantic_ai_slim/pydantic_ai/agent.py index aa01963a8..7fb1e084d 100644 --- a/pydantic_ai_slim/pydantic_ai/agent.py +++ b/pydantic_ai_slim/pydantic_ai/agent.py @@ -1124,7 +1124,7 @@ def _set_result_tool_return(self, return_content: str) -> list[_messages.ModelMe raise LookupError(f'No tool call found with tool name {self.result.tool_name!r}.') @property - def is_ended(self): + def is_ended(self) -> bool: return self.graph_run.is_ended @property diff --git a/pydantic_graph/pydantic_graph/graph.py b/pydantic_graph/pydantic_graph/graph.py index 7724bec2c..710cfedee 100644 --- a/pydantic_graph/pydantic_graph/graph.py +++ b/pydantic_graph/pydantic_graph/graph.py @@ -536,7 +536,7 @@ def __init__( self._result: End[RunEndT] | None = None @property - def is_ended(self): + def is_ended(self) -> bool: return self._result is not None @property @@ -557,6 +557,11 @@ async def next( self: GraphRun[StateT, DepsT, T], node: BaseNode[StateT, DepsT, T] ) -> BaseNode[StateT, DepsT, T] | End[T]: """Note: this method behaves very similarly to an async generator's `asend` method.""" + if not self._started: + raise exceptions.GraphRuntimeError( + 'You must enter the GraphRun as a contextmanager before you can call `next` on it.' + ) + history = self.history state = self.state deps = self.deps