|
7 | 7 | from typing import Annotated, Any, TypeAlias, cast |
8 | 8 |
|
9 | 9 | import requests |
| 10 | +from agents import function_tool |
10 | 11 | from langchain_core.tools import BaseTool |
11 | 12 | from pydantic import BaseModel, BeforeValidator, Field, PrivateAttr |
12 | 13 | from requests.exceptions import RequestException |
@@ -397,6 +398,28 @@ async def _arun(self, **kwargs: Any) -> Any: |
397 | 398 |
|
398 | 399 | return StackOneLangChainTool() |
399 | 400 |
|
| 401 | + def to_openai_agents(self) -> Any: |
| 402 | + """Convert this tool to OpenAI Agents SDK format |
| 403 | +
|
| 404 | + The OpenAI Agents SDK enables building agentic AI apps with lightweight abstractions. |
| 405 | + See: https://openai.github.io/openai-agents-python/ |
| 406 | +
|
| 407 | + Returns: |
| 408 | + Function tool compatible with OpenAI Agents SDK |
| 409 | + """ |
| 410 | + parent_tool = self |
| 411 | + |
| 412 | + async def openai_agents_wrapper(**kwargs: Any) -> JsonDict: |
| 413 | + """Async wrapper for the tool execution compatible with OpenAI Agents SDK""" |
| 414 | + return await parent_tool.acall(kwargs) |
| 415 | + |
| 416 | + # Use the function_tool decorator to create an OpenAI Agents compatible tool |
| 417 | + return function_tool( |
| 418 | + openai_agents_wrapper, |
| 419 | + name_override=self.name, |
| 420 | + description_override=self.description, |
| 421 | + ) |
| 422 | + |
400 | 423 | def set_account_id(self, account_id: str | None) -> None: |
401 | 424 | """Set the account ID for this tool |
402 | 425 |
|
@@ -480,6 +503,17 @@ def to_langchain(self) -> Sequence[BaseTool]: |
480 | 503 | """ |
481 | 504 | return [tool.to_langchain() for tool in self.tools] |
482 | 505 |
|
| 506 | + def to_openai_agents(self) -> list[Any]: |
| 507 | + """Convert all tools to OpenAI Agents SDK format |
| 508 | +
|
| 509 | + The OpenAI Agents SDK enables building agentic AI apps with lightweight abstractions. |
| 510 | + See: https://openai.github.io/openai-agents-python/ |
| 511 | +
|
| 512 | + Returns: |
| 513 | + List of function tools compatible with OpenAI Agents SDK |
| 514 | + """ |
| 515 | + return [tool.to_openai_agents() for tool in self.tools] |
| 516 | + |
483 | 517 | def meta_tools(self) -> "Tools": |
484 | 518 | """Return meta tools for tool discovery and execution |
485 | 519 |
|
|
0 commit comments