Skip to content

Pydantic v2's error object contains a ctx field #86

@kakakikikeke-fork

Description

@kakakikikeke-fork

If you intentionally raise ValueError, a field called ctx seems to be added. An example of pydantic v2 error object.

{'validation_error': {'body_params': [{'ctx': {'error': ValueError()},                                                                       
                                       'input': 'hawksnowlog3',
                                       'loc': ('name',),                                                                                     
                                       'msg': 'Value error, ',                                                                               
                                       'type': 'value_error',                                                                                
                                       'url': 'https://errors.pydantic.dev/2.5/v/value_error'}]}}

An error object is included in ctx and the error object cannot be serialized to dict, resulting in an error. The traceback is this.

Traceback (most recent call last):
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask/app.py", line 1463, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask/app.py", line 872, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask/app.py", line 870, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask/app.py", line 855, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask_pydantic/core.py", line 250, in wrapper
    jsonify({"validation_error": err}), status_code
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask/json/__init__.py", line 170, in jsonify
    return current_app.json.response(*args, **kwargs)  # type: ignore[return-value]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask/json/provider.py", line 216, in response
    f"{self.dumps(obj, **dump_args)}\n", mimetype=self.mimetype
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask/json/provider.py", line 181, in dumps
    return json.dumps(obj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.pyenv/versions/3.11.6/lib/python3.11/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/Users/kakakikikeke/.pyenv/versions/3.11.6/lib/python3.11/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.pyenv/versions/3.11.6/lib/python3.11/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/Users/kakakikikeke/.local/share/virtualenvs/python-try-aR_k1rUJ/lib/python3.11/site-packages/flask/json/provider.py", line 121, in _default
    raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")

I solved it by deleting ctx, is there anything else I can do? #84

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions