|
6 | 6 | from collections.abc import Sequence |
7 | 7 | from datetime import datetime, timezone |
8 | 8 | from enum import Enum |
9 | | -from typing import Annotated, Any, ClassVar, TypeAlias, cast |
| 9 | +from typing import TYPE_CHECKING, Annotated, Any, ClassVar, TypeAlias, cast |
10 | 10 | from urllib.parse import quote |
11 | 11 |
|
12 | 12 | import httpx |
13 | 13 | from langchain_core.tools import BaseTool |
14 | 14 | from pydantic import BaseModel, BeforeValidator, Field, PrivateAttr |
15 | 15 |
|
| 16 | +if TYPE_CHECKING: |
| 17 | + from pydantic_ai.tools import Tool as PydanticAITool |
| 18 | + |
16 | 19 | # Type aliases for common types |
17 | 20 | JsonDict: TypeAlias = dict[str, Any] |
18 | 21 | Headers: TypeAlias = dict[str, str] |
@@ -467,6 +470,36 @@ def _run(self, **kwargs: Any) -> Any: |
467 | 470 |
|
468 | 471 | return StackOneLangChainTool() |
469 | 472 |
|
| 473 | + def to_pydantic_ai_tool(self) -> PydanticAITool: |
| 474 | + """Convert this tool to a Pydantic AI ``Tool``. |
| 475 | +
|
| 476 | + Requires ``stackone-ai[pydantic-ai]`` (installs ``pydantic-ai-slim``). |
| 477 | +
|
| 478 | + Returns: |
| 479 | + A ``pydantic_ai.tools.Tool`` ready to pass to ``Agent(tools=[...])``. |
| 480 | + """ |
| 481 | + try: |
| 482 | + from pydantic_ai.tools import Tool |
| 483 | + except ImportError as e: |
| 484 | + raise ImportError( |
| 485 | + "Install `pydantic-ai-slim` (or `stackone-ai[pydantic-ai]`) " |
| 486 | + "to use the Pydantic AI integration." |
| 487 | + ) from e |
| 488 | + |
| 489 | + openai_function = self.to_openai_function() |
| 490 | + json_schema = openai_function["function"]["parameters"] |
| 491 | + parent_tool = self |
| 492 | + |
| 493 | + def implementation(**kwargs: Any) -> Any: |
| 494 | + return parent_tool.execute(kwargs) |
| 495 | + |
| 496 | + return Tool.from_schema( |
| 497 | + function=implementation, |
| 498 | + name=self.name, |
| 499 | + description=self.description, |
| 500 | + json_schema=json_schema, |
| 501 | + ) |
| 502 | + |
470 | 503 | def set_account_id(self, account_id: str | None) -> None: |
471 | 504 | """Set the account ID for this tool |
472 | 505 |
|
@@ -577,3 +610,13 @@ def to_langchain(self) -> Sequence[BaseTool]: |
577 | 610 | Sequence of tools in LangChain format |
578 | 611 | """ |
579 | 612 | return [tool.to_langchain() for tool in self.tools] |
| 613 | + |
| 614 | + def to_pydantic_ai(self) -> list[PydanticAITool]: |
| 615 | + """Convert all tools to Pydantic AI ``Tool`` instances. |
| 616 | +
|
| 617 | + Requires ``stackone-ai[pydantic-ai]`` (installs ``pydantic-ai-slim``). |
| 618 | +
|
| 619 | + Returns: |
| 620 | + List of ``pydantic_ai.tools.Tool`` ready to pass to ``Agent(tools=[...])``. |
| 621 | + """ |
| 622 | + return [tool.to_pydantic_ai_tool() for tool in self.tools] |
0 commit comments