main
  1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3from typing import TYPE_CHECKING, List, Union, Generic, TypeVar, Optional
  4from typing_extensions import Annotated, TypeAlias
  5
  6from ..._utils import PropertyInfo
  7from .response import Response
  8from ..._models import GenericModel
  9from .response_output_item import (
 10    McpCall,
 11    McpListTools,
 12    LocalShellCall,
 13    McpApprovalRequest,
 14    ImageGenerationCall,
 15    LocalShellCallAction,
 16)
 17from .response_output_text import ResponseOutputText
 18from .response_output_message import ResponseOutputMessage
 19from .response_output_refusal import ResponseOutputRefusal
 20from .response_reasoning_item import ResponseReasoningItem
 21from .response_compaction_item import ResponseCompactionItem
 22from .response_custom_tool_call import ResponseCustomToolCall
 23from .response_computer_tool_call import ResponseComputerToolCall
 24from .response_function_tool_call import ResponseFunctionToolCall
 25from .response_function_web_search import ResponseFunctionWebSearch
 26from .response_apply_patch_tool_call import ResponseApplyPatchToolCall
 27from .response_file_search_tool_call import ResponseFileSearchToolCall
 28from .response_function_shell_tool_call import ResponseFunctionShellToolCall
 29from .response_code_interpreter_tool_call import ResponseCodeInterpreterToolCall
 30from .response_apply_patch_tool_call_output import ResponseApplyPatchToolCallOutput
 31from .response_function_shell_tool_call_output import ResponseFunctionShellToolCallOutput
 32
 33__all__ = ["ParsedResponse", "ParsedResponseOutputMessage", "ParsedResponseOutputText"]
 34
 35ContentType = TypeVar("ContentType")
 36
 37# we need to disable this check because we're overriding properties
 38# with subclasses of their types which is technically unsound as
 39# properties can be mutated.
 40# pyright: reportIncompatibleVariableOverride=false
 41
 42
 43class ParsedResponseOutputText(ResponseOutputText, GenericModel, Generic[ContentType]):
 44    parsed: Optional[ContentType] = None
 45
 46
 47ParsedContent: TypeAlias = Annotated[
 48    Union[ParsedResponseOutputText[ContentType], ResponseOutputRefusal],
 49    PropertyInfo(discriminator="type"),
 50]
 51
 52
 53class ParsedResponseOutputMessage(ResponseOutputMessage, GenericModel, Generic[ContentType]):
 54    if TYPE_CHECKING:
 55        content: List[ParsedContent[ContentType]]  # type: ignore[assignment]
 56    else:
 57        content: List[ParsedContent]
 58
 59
 60class ParsedResponseFunctionToolCall(ResponseFunctionToolCall):
 61    parsed_arguments: object = None
 62
 63    __api_exclude__ = {"parsed_arguments"}
 64
 65
 66ParsedResponseOutputItem: TypeAlias = Annotated[
 67    Union[
 68        ParsedResponseOutputMessage[ContentType],
 69        ParsedResponseFunctionToolCall,
 70        ResponseFileSearchToolCall,
 71        ResponseFunctionWebSearch,
 72        ResponseComputerToolCall,
 73        ResponseReasoningItem,
 74        McpCall,
 75        McpApprovalRequest,
 76        ImageGenerationCall,
 77        LocalShellCall,
 78        LocalShellCallAction,
 79        McpListTools,
 80        ResponseCodeInterpreterToolCall,
 81        ResponseCustomToolCall,
 82        ResponseCompactionItem,
 83        ResponseFunctionShellToolCall,
 84        ResponseFunctionShellToolCallOutput,
 85        ResponseApplyPatchToolCall,
 86        ResponseApplyPatchToolCallOutput,
 87    ],
 88    PropertyInfo(discriminator="type"),
 89]
 90
 91
 92class ParsedResponse(Response, GenericModel, Generic[ContentType]):
 93    if TYPE_CHECKING:
 94        output: List[ParsedResponseOutputItem[ContentType]]  # type: ignore[assignment]
 95    else:
 96        output: List[ParsedResponseOutputItem]
 97
 98    @property
 99    def output_parsed(self) -> Optional[ContentType]:
100        for output in self.output:
101            if output.type == "message":
102                for content in output.content:
103                    if content.type == "output_text" and content.parsed:
104                        return content.parsed
105
106        return None