Skip to content

Commit 017905c

Browse files
authored
Change Agent.is_?_node() to use TypeIs instead (#1622)
1 parent d5e09dc commit 017905c

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

pydantic_ai_slim/pydantic_ai/agent.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from opentelemetry.trace import NoOpTracer, use_span
1414
from pydantic.json_schema import GenerateJsonSchema
15-
from typing_extensions import Literal, Never, TypeGuard, TypeVar, deprecated
15+
from typing_extensions import Literal, Never, TypeIs, TypeVar, deprecated
1616

1717
from pydantic_graph import End, Graph, GraphRun, GraphRunContext
1818
from pydantic_graph._utils import get_event_loop
@@ -459,7 +459,7 @@ def iter(
459459
self,
460460
user_prompt: str | Sequence[_messages.UserContent] | None,
461461
*,
462-
output_type: type[RunOutputDataT] | ToolOutput[RunOutputDataT] | None = None,
462+
output_type: None = None,
463463
message_history: list[_messages.ModelMessage] | None = None,
464464
model: models.Model | models.KnownModelName | str | None = None,
465465
deps: AgentDepsT = None,
@@ -468,7 +468,23 @@ def iter(
468468
usage: _usage.Usage | None = None,
469469
infer_name: bool = True,
470470
**_deprecated_kwargs: Never,
471-
) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, Any]]: ...
471+
) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]]: ...
472+
473+
@overload
474+
def iter(
475+
self,
476+
user_prompt: str | Sequence[_messages.UserContent] | None,
477+
*,
478+
output_type: type[RunOutputDataT] | ToolOutput[RunOutputDataT],
479+
message_history: list[_messages.ModelMessage] | None = None,
480+
model: models.Model | models.KnownModelName | str | None = None,
481+
deps: AgentDepsT = None,
482+
model_settings: ModelSettings | None = None,
483+
usage_limits: _usage.UsageLimits | None = None,
484+
usage: _usage.Usage | None = None,
485+
infer_name: bool = True,
486+
**_deprecated_kwargs: Never,
487+
) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]]: ...
472488

473489
@overload
474490
@deprecated('`result_type` is deprecated, use `output_type` instead.')
@@ -1621,7 +1637,7 @@ def _prepare_output_schema(
16211637
@staticmethod
16221638
def is_model_request_node(
16231639
node: _agent_graph.AgentNode[T, S] | End[result.FinalResult[S]],
1624-
) -> TypeGuard[_agent_graph.ModelRequestNode[T, S]]:
1640+
) -> TypeIs[_agent_graph.ModelRequestNode[T, S]]:
16251641
"""Check if the node is a `ModelRequestNode`, narrowing the type if it is.
16261642
16271643
This method preserves the generic parameters while narrowing the type, unlike a direct call to `isinstance`.
@@ -1631,7 +1647,7 @@ def is_model_request_node(
16311647
@staticmethod
16321648
def is_call_tools_node(
16331649
node: _agent_graph.AgentNode[T, S] | End[result.FinalResult[S]],
1634-
) -> TypeGuard[_agent_graph.CallToolsNode[T, S]]:
1650+
) -> TypeIs[_agent_graph.CallToolsNode[T, S]]:
16351651
"""Check if the node is a `CallToolsNode`, narrowing the type if it is.
16361652
16371653
This method preserves the generic parameters while narrowing the type, unlike a direct call to `isinstance`.
@@ -1641,7 +1657,7 @@ def is_call_tools_node(
16411657
@staticmethod
16421658
def is_user_prompt_node(
16431659
node: _agent_graph.AgentNode[T, S] | End[result.FinalResult[S]],
1644-
) -> TypeGuard[_agent_graph.UserPromptNode[T, S]]:
1660+
) -> TypeIs[_agent_graph.UserPromptNode[T, S]]:
16451661
"""Check if the node is a `UserPromptNode`, narrowing the type if it is.
16461662
16471663
This method preserves the generic parameters while narrowing the type, unlike a direct call to `isinstance`.
@@ -1651,7 +1667,7 @@ def is_user_prompt_node(
16511667
@staticmethod
16521668
def is_end_node(
16531669
node: _agent_graph.AgentNode[T, S] | End[result.FinalResult[S]],
1654-
) -> TypeGuard[End[result.FinalResult[S]]]:
1670+
) -> TypeIs[End[result.FinalResult[S]]]:
16551671
"""Check if the node is a `End`, narrowing the type if it is.
16561672
16571673
This method preserves the generic parameters while narrowing the type, unlike a direct call to `isinstance`.

0 commit comments

Comments
 (0)