pydantic-ai-slim 0.0.55__py3-none-any.whl → 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,9 +22,9 @@ from ..messages import (
22
22
  ToolCallPart,
23
23
  ToolReturnPart,
24
24
  )
25
- from ..result import Usage
26
25
  from ..settings import ModelSettings
27
26
  from ..tools import ToolDefinition
27
+ from ..usage import Usage
28
28
  from . import (
29
29
  Model,
30
30
  ModelRequestParameters,
@@ -34,15 +34,15 @@ from .function import _estimate_string_tokens, _estimate_usage # pyright: ignor
34
34
 
35
35
 
36
36
  @dataclass
37
- class _TextResult:
38
- """A private wrapper class to tag a result that came from the custom_result_text field."""
37
+ class _WrappedTextOutput:
38
+ """A private wrapper class to tag an output that came from the custom_output_text field."""
39
39
 
40
40
  value: str | None
41
41
 
42
42
 
43
43
  @dataclass
44
- class _FunctionToolResult:
45
- """A wrapper class to tag a result that came from the custom_result_args field."""
44
+ class _WrappedToolOutput:
45
+ """A wrapper class to tag an output that came from the custom_output_args field."""
46
46
 
47
47
  value: Any | None
48
48
 
@@ -65,16 +65,16 @@ class TestModel(Model):
65
65
 
66
66
  call_tools: list[str] | Literal['all'] = 'all'
67
67
  """List of tools to call. If `'all'`, all tools will be called."""
68
- custom_result_text: str | None = None
69
- """If set, this text is returned as the final result."""
70
- custom_result_args: Any | None = None
71
- """If set, these args will be passed to the result tool."""
68
+ custom_output_text: str | None = None
69
+ """If set, this text is returned as the final output."""
70
+ custom_output_args: Any | None = None
71
+ """If set, these args will be passed to the output tool."""
72
72
  seed: int = 0
73
73
  """Seed for generating random data."""
74
74
  last_model_request_parameters: ModelRequestParameters | None = field(default=None, init=False)
75
75
  """The last ModelRequestParameters passed to the model in a request.
76
76
 
77
- The ModelRequestParameters contains information about the function and result tools available during request handling.
77
+ The ModelRequestParameters contains information about the function and output tools available during request handling.
78
78
 
79
79
  This is set when a request is made, so will reflect the function tools from the last step of the last run.
80
80
  """
@@ -88,7 +88,6 @@ class TestModel(Model):
88
88
  model_request_parameters: ModelRequestParameters,
89
89
  ) -> tuple[ModelResponse, Usage]:
90
90
  self.last_model_request_parameters = model_request_parameters
91
-
92
91
  model_response = self._request(messages, model_settings, model_request_parameters)
93
92
  usage = _estimate_usage([*messages, model_response])
94
93
  return model_response, usage
@@ -128,29 +127,29 @@ class TestModel(Model):
128
127
  tools_to_call = (function_tools_lookup[name] for name in self.call_tools)
129
128
  return [(r.name, r) for r in tools_to_call]
130
129
 
131
- def _get_result(self, model_request_parameters: ModelRequestParameters) -> _TextResult | _FunctionToolResult:
132
- if self.custom_result_text is not None:
133
- assert model_request_parameters.allow_text_result, (
134
- 'Plain response not allowed, but `custom_result_text` is set.'
130
+ def _get_output(self, model_request_parameters: ModelRequestParameters) -> _WrappedTextOutput | _WrappedToolOutput:
131
+ if self.custom_output_text is not None:
132
+ assert model_request_parameters.allow_text_output, (
133
+ 'Plain response not allowed, but `custom_output_text` is set.'
135
134
  )
136
- assert self.custom_result_args is None, 'Cannot set both `custom_result_text` and `custom_result_args`.'
137
- return _TextResult(self.custom_result_text)
138
- elif self.custom_result_args is not None:
139
- assert model_request_parameters.result_tools is not None, (
140
- 'No result tools provided, but `custom_result_args` is set.'
135
+ assert self.custom_output_args is None, 'Cannot set both `custom_output_text` and `custom_output_args`.'
136
+ return _WrappedTextOutput(self.custom_output_text)
137
+ elif self.custom_output_args is not None:
138
+ assert model_request_parameters.output_tools is not None, (
139
+ 'No output tools provided, but `custom_output_args` is set.'
141
140
  )
142
- result_tool = model_request_parameters.result_tools[0]
141
+ output_tool = model_request_parameters.output_tools[0]
143
142
 
144
- if k := result_tool.outer_typed_dict_key:
145
- return _FunctionToolResult({k: self.custom_result_args})
143
+ if k := output_tool.outer_typed_dict_key:
144
+ return _WrappedToolOutput({k: self.custom_output_args})
146
145
  else:
147
- return _FunctionToolResult(self.custom_result_args)
148
- elif model_request_parameters.allow_text_result:
149
- return _TextResult(None)
150
- elif model_request_parameters.result_tools:
151
- return _FunctionToolResult(None)
146
+ return _WrappedToolOutput(self.custom_output_args)
147
+ elif model_request_parameters.allow_text_output:
148
+ return _WrappedTextOutput(None)
149
+ elif model_request_parameters.output_tools:
150
+ return _WrappedToolOutput(None)
152
151
  else:
153
- return _TextResult(None)
152
+ return _WrappedTextOutput(None) # pragma: no cover
154
153
 
155
154
  def _request(
156
155
  self,
@@ -159,8 +158,8 @@ class TestModel(Model):
159
158
  model_request_parameters: ModelRequestParameters,
160
159
  ) -> ModelResponse:
161
160
  tool_calls = self._get_tool_calls(model_request_parameters)
162
- result = self._get_result(model_request_parameters)
163
- result_tools = model_request_parameters.result_tools
161
+ output_wrapper = self._get_output(model_request_parameters)
162
+ output_tools = model_request_parameters.output_tools
164
163
 
165
164
  # if there are tools, the first thing we want to do is call all of them
166
165
  if tool_calls and not any(isinstance(m, ModelResponse) for m in messages):
@@ -176,29 +175,29 @@ class TestModel(Model):
176
175
  # check if there are any retry prompts, if so retry them
177
176
  new_retry_names = {p.tool_name for p in last_message.parts if isinstance(p, RetryPromptPart)}
178
177
  if new_retry_names:
179
- # Handle retries for both function tools and result tools
178
+ # Handle retries for both function tools and output tools
180
179
  # Check function tools first
181
180
  retry_parts: list[ModelResponsePart] = [
182
181
  ToolCallPart(name, self.gen_tool_args(args)) for name, args in tool_calls if name in new_retry_names
183
182
  ]
184
- # Check result tools
185
- if result_tools:
183
+ # Check output tools
184
+ if output_tools:
186
185
  retry_parts.extend(
187
186
  [
188
187
  ToolCallPart(
189
188
  tool.name,
190
- result.value
191
- if isinstance(result, _FunctionToolResult) and result.value is not None
189
+ output_wrapper.value
190
+ if isinstance(output_wrapper, _WrappedToolOutput) and output_wrapper.value is not None
192
191
  else self.gen_tool_args(tool),
193
192
  )
194
- for tool in result_tools
193
+ for tool in output_tools
195
194
  if tool.name in new_retry_names
196
195
  ]
197
196
  )
198
197
  return ModelResponse(parts=retry_parts, model_name=self._model_name)
199
198
 
200
- if isinstance(result, _TextResult):
201
- if (response_text := result.value) is None:
199
+ if isinstance(output_wrapper, _WrappedTextOutput):
200
+ if (response_text := output_wrapper.value) is None:
202
201
  # build up details of tool responses
203
202
  output: dict[str, Any] = {}
204
203
  for message in messages:
@@ -215,16 +214,16 @@ class TestModel(Model):
215
214
  else:
216
215
  return ModelResponse(parts=[TextPart(response_text)], model_name=self._model_name)
217
216
  else:
218
- assert result_tools, 'No result tools provided'
219
- custom_result_args = result.value
220
- result_tool = result_tools[self.seed % len(result_tools)]
221
- if custom_result_args is not None:
217
+ assert output_tools, 'No output tools provided'
218
+ custom_output_args = output_wrapper.value
219
+ output_tool = output_tools[self.seed % len(output_tools)]
220
+ if custom_output_args is not None:
222
221
  return ModelResponse(
223
- parts=[ToolCallPart(result_tool.name, custom_result_args)], model_name=self._model_name
222
+ parts=[ToolCallPart(output_tool.name, custom_output_args)], model_name=self._model_name
224
223
  )
225
224
  else:
226
- response_args = self.gen_tool_args(result_tool)
227
- return ModelResponse(parts=[ToolCallPart(result_tool.name, response_args)], model_name=self._model_name)
225
+ response_args = self.gen_tool_args(output_tool)
226
+ return ModelResponse(parts=[ToolCallPart(output_tool.name, response_args)], model_name=self._model_name)
228
227
 
229
228
 
230
229
  @dataclass