← pod_r5_v1h

fastapi_14791

resolved RESOLVED PASS · 7 tool calls · 64 s · fastapi/fastapi

Task input

🐛 Update `ValidationError` schema to include `input` and `ctx`

## Summary

Related to #10787

The OpenAPI `ValidationError` schema was outdated and didn't reflect the actual error format returned by Pydantic V2.

## Changes
Updated `validation_error_definition` in `fastapi/openapi/utils.py` to include:
- `input`: The actual input value that caused the validation error  
- `ctx`: Optional context dictionary with additional error information

## Testing
All existing tests pass with the updated schema. The test snapshots have been updated to reflect the new schema structure.

Tool calls (8)

#ToolArgumentsResult
1run_command
{
 "command": "grep -n \"validation_error_definition\" fastapi/openapi/utils.py"
}
{"status": "ok", "stdout": "41:validation_error_definition = {\n435: \"ValidationError\": validation_error_definition,\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "41:validation_error_definition = {\n435:                            \"ValidationError\": validation_error_definition,\n", "stderr": "", "exit_code": 0}
2run_command
{
 "command": "sed -n '41,50p' fastapi/openapi/utils.py"
}
{"status": "ok", "stdout": "validation_error_definition = {\n \"title\": \"ValidationError\",\n \"type\": \"object\",\n \"properties\": {\n \"loc\": {\n \"title
full
{"status": "ok", "stdout": "validation_error_definition = {\n    \"title\": \"ValidationError\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"loc\": {\n            \"title\": \"Location\",\n            \"type\": \"array\",\n            \"items\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]},\n        },\n        \"msg\": {\"title\": \"Message\", \"type\": \"string\"},\n", "stderr": "", "exit_code": 0}
3run_command
{
 "command": "sed -n '41,60p' fastapi/openapi/utils.py"
}
{"status": "ok", "stdout": "validation_error_definition = {\n \"title\": \"ValidationError\",\n \"type\": \"object\",\n \"properties\": {\n \"loc\": {\n \"title
full
{"status": "ok", "stdout": "validation_error_definition = {\n    \"title\": \"ValidationError\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"loc\": {\n            \"title\": \"Location\",\n            \"type\": \"array\",\n            \"items\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]},\n        },\n        \"msg\": {\"title\": \"Message\", \"type\": \"string\"},\n        \"type\": {\"title\": \"Error Type\", \"type\": \"string\"},\n    },\n    \"required\": [\"loc\", \"msg\", \"type\"],\n}\n\nvalidation_error_response_definition = {\n    \"title\": \"HTTPValidationError\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"detail\": {\n", "stderr": "", "exit_code": 0}
4run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/openapi/utils.py\")\ns = p.read_text()\nold = \"\"\"validation_error_definition = {\n    \"title\": \"ValidationError\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"loc\": {\n            \"title\": \"Location\",\n            \"type\": \"array\",\n            \"items\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]},\n        },\n        \"msg\": {\"title\": \"Message\", \"type\": \"string\"},\n        \"type\": {\"title\": \"Error Type\", \"type\": \"string\"},\n    },\n    \"required\": [\"loc\", \"msg\", \"type\"],\n}\"\"\"\nnew = \"\"\"validation_error_definition = {\n    \"title\": \"ValidationError\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"loc\": {\n            \"title\": \"Location\",\n            \"type\": \"array\",\n            \"items\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]},\n        },\n        \"msg\": {\"title\": \"Message\", \"type\": \"string\"},\n        \"type\": {\"title\": \"Error Type\", \"type\": \"string\"},\n        \"input\": {\"title\": \"Input\", \"type\": \"anyOf\", \"items\": [{\"type\": \"string\"}, {\"type\": \"integer\"}, {\"type\": \"object\"}, {\"type\": \"array\"}]},\n        \"ctx\": {\"title\": \"Context\", \"type\": \"object\", \"nullable\": True},\n    },\n    \"required\": [\"loc\", \"msg\", \"type\"],\n}\"\"\"\n# Wait, \"anyOf\" is not a type in JSON Schema, it should be a separate key.\n# Looking at the 'loc' item: \"items\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]}\n# So 'input' should probably be:\n# \"input\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}, {\"type\": \"object\"}, {\"type\": \"array\"}]}\n# But looking at Pydantic V2, input can be anything.\n# In JSON Schema, any type is represented by omitting 'type' or using an empty schema {}.\n# However, to be explicit and follow the style of 'loc' items:\n# \"input\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}, {\"type\": \"object\"}, {\"type\": \"array\"}, {\"type\": \"boolean\"}, {\"type\": \"null\"}]}\n# Let's re-evaluate 'input' type.\nPY\n"
}
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
5run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/openapi/utils.py\")\ns = p.read_text()\nold = \"\"\"validation_error_definition = {\n    \"title\": \"ValidationError\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"loc\": {\n            \"title\": \"Location\",\n            \"type\": \"array\",\n            \"items\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]},\n        },\n        \"msg\": {\"title\": \"Message\", \"type\": \"string\"},\n        \"type\": {\"title\": \"Error Type\", \"type\": \"string\"},\n    },\n    \"required\": [\"loc\", \"msg\", \"type\"],\n}\"\"\"\nnew = \"\"\"validation_error_definition = {\n    \"title\": \"ValidationError\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"loc\": {\n            \"title\": \"Location\",\n            \"type\": \"array\",\n            \"items\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]},\n        },\n        \"msg\": {\"title\": \"Message\", \"type\": \"string\"},\n        \"type\": {\"title\": \"Error Type\", \"type\": \"string\"},\n        \"input\": {\"title\": \"Input\"},\n        \"ctx\": {\"title\": \"Context\", \"type\": \"object\"},\n    },\n    \"required\": [\"loc\", \"msg\", \"type\"],\n}\"\"\"\nassert s.count(old) == 1, (\"old text found this many times:\", s.count(old))\nt = s.replace(old, new)\ncompile(t, str(p), \"exec\")\np.write_text(t)\nPY\n"
}
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
6run_command
{
 "command": "sed -n '41,60p' fastapi/openapi/utils.py"
}
{"status": "ok", "stdout": "validation_error_definition = {\n \"title\": \"ValidationError\",\n \"type\": \"object\",\n \"properties\": {\n \"loc\": {\n \"title
full
{"status": "ok", "stdout": "validation_error_definition = {\n    \"title\": \"ValidationError\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"loc\": {\n            \"title\": \"Location\",\n            \"type\": \"array\",\n            \"items\": {\"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]},\n        },\n        \"msg\": {\"title\": \"Message\", \"type\": \"string\"},\n        \"type\": {\"title\": \"Error Type\", \"type\": \"string\"},\n        \"input\": {\"title\": \"Input\"},\n        \"ctx\": {\"title\": \"Context\", \"type\": \"object\"},\n    },\n    \"required\": [\"loc\", \"msg\", \"type\"],\n}\n\nvalidation_error_response_definition = {\n    \"title\": \"HTTPValidationError\",\n    \"type\": \"object\",\n", "stderr": "", "exit_code": 0}
7run_command
{
 "command": "git diff"
}
{"status": "ok", "stdout": "diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py\nindex 75ff2610..29f03bd2 100644\n--- a/fastapi/openapi/utils.py\n+
full
{"status": "ok", "stdout": "diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py\nindex 75ff2610..29f03bd2 100644\n--- a/fastapi/openapi/utils.py\n+++ b/fastapi/openapi/utils.py\n@@ -49,6 +49,8 @@ validation_error_definition = {\n         },\n         \"msg\": {\"title\": \"Message\", \"type\": \"string\"},\n         \"type\": {\"title\": \"Error Type\", \"type\": \"string\"},\n+        \"input\": {\"title\": \"Input\"},\n+        \"ctx\": {\"title\": \"Context\", \"type\": \"object\"},\n     },\n     \"required\": [\"loc\", \"msg\", \"type\"],\n }\n", "stderr": "", "exit_code": 0}
8submit_patch
{}
{"status": "ok", "patch_size": 483, "files_changed": 1}
full
{"status": "ok", "patch_size": 483, "files_changed": 1}

Patch

diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py
index 75ff2610..29f03bd2 100644
--- a/fastapi/openapi/utils.py
+++ b/fastapi/openapi/utils.py
@@ -49,6 +49,8 @@ validation_error_definition = {
         },
         "msg": {"title": "Message", "type": "string"},
         "type": {"title": "Error Type", "type": "string"},
+        "input": {"title": "Input"},
+        "ctx": {"title": "Context", "type": "object"},
     },
     "required": ["loc", "msg", "type"],
 }

Test output

show
........................................................................ [ 43%]
........................................................................ [ 87%]
.....................                                                    [100%]
=============================== warnings summary ===============================
../../../../../../../Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.50.0-py3-none-any/starlette/testclient.py:45
  /Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.50.0-py3-none-any/starlette/testclient.py:45: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
    _PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
165 passed, 1 warning in 1.83s