8
8
from datetime import datetime , timezone
9
9
from typing import Literal , Union , cast , overload
10
10
11
- from openai import NotGiven
12
- from openai .types import Reasoning
13
11
from typing_extensions import assert_never
14
12
15
13
from pydantic_ai .providers import Provider , infer_provider
44
42
)
45
43
46
44
try :
47
- from openai import NOT_GIVEN , APIStatusError , AsyncOpenAI , AsyncStream
45
+ from openai import NOT_GIVEN , APIStatusError , AsyncOpenAI , AsyncStream , NotGiven
48
46
from openai .types import ChatModel , chat , responses
49
47
from openai .types .chat import (
50
48
ChatCompletionChunk ,
@@ -95,8 +93,7 @@ class OpenAIModelSettings(ModelSettings, total=False):
95
93
"""
96
94
97
95
openai_reasoning_effort : ReasoningEffort
98
- """
99
- Constrains effort on reasoning for [reasoning models](https://platform.openai.com/docs/guides/reasoning).
96
+ """Constrains effort on reasoning for [reasoning models](https://platform.openai.com/docs/guides/reasoning).
100
97
101
98
Currently supported values are `low`, `medium`, and `high`. Reducing reasoning effort can
102
99
result in faster responses and fewer tokens used on reasoning in a response.
@@ -121,6 +118,27 @@ class OpenAIResponsesModelSettings(OpenAIModelSettings, total=False):
121
118
See [OpenAI's built-in tools](https://platform.openai.com/docs/guides/tools?api-mode=responses) for more details.
122
119
"""
123
120
121
+ openai_reasoning_generate_summary : Literal ['detailed' , 'concise' ]
122
+ """A summary of the reasoning performed by the model.
123
+
124
+ This can be useful for debugging and understanding the model's reasoning process.
125
+ One of `concise` or `detailed`.
126
+
127
+ Check the [OpenAI Computer use documentation](https://platform.openai.com/docs/guides/tools-computer-use#1-send-a-request-to-the-model)
128
+ for more details.
129
+ """
130
+
131
+ openai_truncation : Literal ['disabled' , 'auto' ]
132
+ """The truncation strategy to use for the model response.
133
+
134
+ It can be either:
135
+ - `disabled` (default): If a model response will exceed the context window size for a model, the
136
+ request will fail with a 400 error.
137
+ - `auto`: If the context of this response and previous ones exceeds the model's context window size,
138
+ the model will truncate the response to fit the context window by dropping input items in the
139
+ middle of the conversation.
140
+ """
141
+
124
142
125
143
@dataclass (init = False )
126
144
class OpenAIModel (Model ):
@@ -567,12 +585,7 @@ async def _responses_create(
567
585
tool_choice = 'auto'
568
586
569
587
system_prompt , openai_messages = await self ._map_message (messages )
570
-
571
- reasoning_effort = model_settings .get ('openai_reasoning_effort' , NOT_GIVEN )
572
- if not isinstance (reasoning_effort , NotGiven ):
573
- reasoning = Reasoning (effort = reasoning_effort )
574
- else :
575
- reasoning = NOT_GIVEN
588
+ reasoning = self ._get_reasoning (model_settings )
576
589
577
590
try :
578
591
return await self .client .responses .create (
@@ -586,6 +599,7 @@ async def _responses_create(
586
599
stream = stream ,
587
600
temperature = model_settings .get ('temperature' , NOT_GIVEN ),
588
601
top_p = model_settings .get ('top_p' , NOT_GIVEN ),
602
+ truncation = model_settings .get ('openai_truncation' , NOT_GIVEN ),
589
603
timeout = model_settings .get ('timeout' , NOT_GIVEN ),
590
604
reasoning = reasoning ,
591
605
user = model_settings .get ('user' , NOT_GIVEN ),
@@ -595,6 +609,14 @@ async def _responses_create(
595
609
raise ModelHTTPError (status_code = status_code , model_name = self .model_name , body = e .body ) from e
596
610
raise
597
611
612
+ def _get_reasoning (self , model_settings : OpenAIResponsesModelSettings ) -> Reasoning | NotGiven :
613
+ reasoning_effort = model_settings .get ('openai_reasoning_effort' , None )
614
+ reasoning_generate_summary = model_settings .get ('openai_reasoning_generate_summary' , None )
615
+
616
+ if reasoning_effort is None and reasoning_generate_summary is None :
617
+ return NOT_GIVEN
618
+ return Reasoning (effort = reasoning_effort , generate_summary = reasoning_generate_summary )
619
+
598
620
def _get_tools (self , model_request_parameters : ModelRequestParameters ) -> list [responses .FunctionToolParam ]:
599
621
tools = [self ._map_tool_definition (r ) for r in model_request_parameters .function_tools ]
600
622
if model_request_parameters .result_tools :
0 commit comments