Skip to content

Commit dad1994

Browse files
authored
Improve validation error retry message (#3193)
1 parent 5fa446a commit dad1994

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,10 @@ def model_response(self) -> str:
888888
description = self.content
889889
else:
890890
json_errors = error_details_ta.dump_json(self.content, exclude={'__all__': {'ctx'}}, indent=2)
891-
description = f'{len(self.content)} validation errors: {json_errors.decode()}'
891+
plural = isinstance(self.content, list) and len(self.content) != 1
892+
description = (
893+
f'{len(self.content)} validation error{"s" if plural else ""}:\n```json\n{json_errors.decode()}\n```'
894+
)
892895
return f'{description}\n\nFix the errors and try again.'
893896

894897
def otel_event(self, settings: InstrumentationSettings) -> Event:

tests/models/cassettes/test_gemini/test_gemini_drop_exclusive_maximum.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ interactions:
259259
name: get_chinese_zodiac
260260
response:
261261
call_error: |-
262-
1 validation errors: [
262+
1 validation error: [
263263
{
264264
"type": "greater_than",
265265
"loc": [

tests/test_agent.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def return_model(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse
201201
),
202202
ModelResponse(
203203
parts=[ToolCallPart(tool_name='final_result', args='{"a": 42, "b": "foo"}', tool_call_id=IsStr())],
204-
usage=RequestUsage(input_tokens=87, output_tokens=14),
204+
usage=RequestUsage(input_tokens=89, output_tokens=14),
205205
model_name='function:return_model:',
206206
timestamp=IsNow(tz=timezone.utc),
207207
),
@@ -260,7 +260,9 @@ def check_b(cls, v: str) -> str:
260260
retry_prompt = user_retry.parts[0]
261261
assert isinstance(retry_prompt, RetryPromptPart)
262262
assert retry_prompt.model_response() == snapshot("""\
263-
1 validation errors: [
263+
1 validation error:
264+
```json
265+
[
264266
{
265267
"type": "value_error",
266268
"loc": [
@@ -270,6 +272,7 @@ def check_b(cls, v: str) -> str:
270272
"input": "foo"
271273
}
272274
]
275+
```
273276
274277
Fix the errors and try again.""")
275278

@@ -1796,7 +1799,7 @@ class CityLocation(BaseModel):
17961799
),
17971800
ModelResponse(
17981801
parts=[TextPart(content='{"city": "Mexico City", "country": "Mexico"}')],
1799-
usage=RequestUsage(input_tokens=85, output_tokens=12),
1802+
usage=RequestUsage(input_tokens=87, output_tokens=12),
18001803
model_name='function:return_city_location:',
18011804
timestamp=IsDatetime(),
18021805
),

0 commit comments

Comments
 (0)