|
15 | 15 | from openai.types.responses.tool_param import CodeInterpreter, ImageGeneration, Mcp |
16 | 16 | from openai.types.responses.web_search_tool import Filters as WebSearchToolFilters |
17 | 17 | from openai.types.responses.web_search_tool_param import UserLocation |
18 | | -from pydantic import ValidationError |
| 18 | +from pydantic import BaseModel, TypeAdapter, ValidationError |
19 | 19 | from typing_extensions import Concatenate, NotRequired, ParamSpec, TypedDict |
20 | 20 |
|
21 | 21 | from . import _debug |
22 | 22 | from .computer import AsyncComputer, Computer |
23 | 23 | from .exceptions import ModelBehaviorError |
24 | 24 | from .function_schema import DocstringStyle, function_schema |
25 | | -from .items import RunItem |
26 | 25 | from .logger import logger |
27 | 26 | from .run_context import RunContextWrapper |
28 | 27 | from .strict_schema import ensure_strict_json_schema |
|
34 | 33 |
|
35 | 34 | if TYPE_CHECKING: |
36 | 35 | from .agent import Agent, AgentBase |
| 36 | + from .items import RunItem |
| 37 | + |
37 | 38 |
|
38 | 39 | ToolParams = ParamSpec("ToolParams") |
39 | 40 |
|
|
48 | 49 | ] |
49 | 50 |
|
50 | 51 |
|
| 52 | +class ToolOutputText(BaseModel): |
| 53 | + """Represents a tool output that should be sent to the model as text.""" |
| 54 | + |
| 55 | + type: Literal["text"] = "text" |
| 56 | + text: str |
| 57 | + |
| 58 | + |
| 59 | +class ToolOutputTextDict(TypedDict, total=False): |
| 60 | + """TypedDict variant for text tool outputs.""" |
| 61 | + |
| 62 | + type: Literal["text"] |
| 63 | + text: str |
| 64 | + |
| 65 | + |
| 66 | +class ToolOutputImage(BaseModel): |
| 67 | + """Represents a tool output that should be sent to the model as an image. |
| 68 | +
|
| 69 | + You can provide either an `image_url` (URL or data URL) or a `file_id` for previously uploaded |
| 70 | + content. The optional `detail` can control vision detail. |
| 71 | + """ |
| 72 | + |
| 73 | + type: Literal["image"] = "image" |
| 74 | + image_url: str | None = None |
| 75 | + file_id: str | None = None |
| 76 | + detail: Literal["low", "high", "auto"] | None = None |
| 77 | + |
| 78 | + |
| 79 | +class ToolOutputImageDict(TypedDict, total=False): |
| 80 | + """TypedDict variant for image tool outputs.""" |
| 81 | + |
| 82 | + type: Literal["image"] |
| 83 | + image_url: NotRequired[str] |
| 84 | + file_id: NotRequired[str] |
| 85 | + detail: NotRequired[Literal["low", "high", "auto"]] |
| 86 | + |
| 87 | + |
| 88 | +class ToolOutputFileContent(BaseModel): |
| 89 | + """Represents a tool output that should be sent to the model as a file. |
| 90 | +
|
| 91 | + Provide one of `file_data` (base64), `file_url`, or `file_id`. You may also |
| 92 | + provide an optional `filename` when using `file_data` to hint file name. |
| 93 | + """ |
| 94 | + |
| 95 | + type: Literal["file"] = "file" |
| 96 | + file_data: str | None = None |
| 97 | + file_url: str | None = None |
| 98 | + file_id: str | None = None |
| 99 | + filename: str | None = None |
| 100 | + |
| 101 | + |
| 102 | +class ToolOutputFileContentDict(TypedDict, total=False): |
| 103 | + """TypedDict variant for file content tool outputs.""" |
| 104 | + |
| 105 | + type: Literal["file"] |
| 106 | + file_data: NotRequired[str] |
| 107 | + file_url: NotRequired[str] |
| 108 | + file_id: NotRequired[str] |
| 109 | + filename: NotRequired[str] |
| 110 | + |
| 111 | + |
| 112 | +ValidToolOutputPydanticModels = Union[ToolOutputText, ToolOutputImage, ToolOutputFileContent] |
| 113 | +ValidToolOutputPydanticModelsTypeAdapter: TypeAdapter[ValidToolOutputPydanticModels] = TypeAdapter( |
| 114 | + ValidToolOutputPydanticModels |
| 115 | +) |
| 116 | + |
| 117 | + |
51 | 118 | @dataclass |
52 | 119 | class FunctionToolResult: |
53 | 120 | tool: FunctionTool |
@@ -81,7 +148,9 @@ class FunctionTool: |
81 | 148 | 1. The tool run context. |
82 | 149 | 2. The arguments from the LLM, as a JSON string. |
83 | 150 |
|
84 | | - You must return a string representation of the tool output, or something we can call `str()` on. |
| 151 | + You must return a one of the structured tool output types (e.g. ToolOutputText, ToolOutputImage, |
| 152 | + ToolOutputFileContent) or a string representation of the tool output, or a list of them, |
| 153 | + or something we can call `str()` on. |
85 | 154 | In case of errors, you can either raise an Exception (which will cause the run to fail) or |
86 | 155 | return a string error message (which will be sent back to the LLM). |
87 | 156 | """ |
|
0 commit comments