Skip to main content

Human-readable verbose error messages for Pydantic validation errors

Project description

Pydantic Nicer Error Classes

This package returns nicer pydantic errors on over 40+ ValidationError types that are more human readable.

For example, with a json error you might get the following with pydantic:

ValidationError: 1 validation error for User
  Invalid JSON: expected value at line 1 column 25 [type=json_invalid, input_value='{"name": "John", "age": ....com", "addresses": []}', input_type=str]
    For further information visit https://errors.pydantic.dev/2.12/v/json_invalid

This can be hard to read, and unclear to non-technical users. Producing the same error with the package the pydantic-error-handling package becomes much clearer, with clear highlighting of the specific issue.

Invalid JSON at line 1, column 25:
  {"name": "John", "age": invalid, "email": "test@example.com", "addresses": []}
                          ^ expected value

Quick Start

from pydantic import BaseModel
from pydantic_error_handling import verbose_errors

@verbose_errors
class User(BaseModel):
    name: str
    age: int
    email: str

# JSON parsing errors now show visual arrows
bad_json = '{"name": "John", "age": invalid, "email": "test@example.com"}'
try:
    User.model_validate_json(bad_json)
except Exception as e:
    print(e)
    # Invalid JSON at line 1, column 25:
    #   {"name": "John", "age": invalid, "email": "test@example.com"}
    #                           ^ expected value

How to use this package

Option 1: Use @verbose_errors wrapper

  1. install the package to your project using pip install pydantic-error-handling

  2. Add the decorator verbose_errors to return nicely typed errors:

from pydantic_error_handling import verbose_errors


@verbose_errors
class MyPydanticClass(pydantic.BaseModel):
   ...

note: to produce nice loc functions, this package automatically removes typing and validators from the pattern (e.g function-before, enum etc.). To add additional omissions, you can use the omit_patterns param:

from pydantic_error_handling import verbose_errors


@verbose_errors(omit_patterns=["Problem"])
class MyPydanticClass(pydantic.BaseModel):
   ...

Option 2: Use helper functions to translate errors

  1. install the package to your project using pip install pydantic-error-handling

  2. generate the relevant pydantic error

  3. pass the error as a NicePydanticError class or string

from pydantic import BaseModel, ValidationError
from pydantic_error_handling import error_to_nice, error_to_string

class MyModel(BaseModel):
    name: str
    age: int

try:
    MyModel(name=123, age="invalid")
except ValidationError as e:
    # Get human-readable string
    print(error_to_string(e))
    # Output: 'name': Input should be a valid string. Received type: int, value: 123
    #         'age': Input should be a valid integer. Received type: str, value: 'invalid'
    
    # Or get structured errors for API/UI
    nice_errors = error_to_nice(e)
    for err in nice_errors:
        print(f"Field: {err.field_path}, Message: {err.message}")

You can also use a helper function to handle deeply nested Pydantic Errors:

from pydantic import BaseModel, field_validator, ValidationError
from pydantic_error_handling import nested_error_to_nice

class Address(BaseModel):
    postcode: str = Field(pattern=r"^\d{5}$")
    street: str

class Order(BaseModel):
    ref: str

    @field_validator("ref", mode="before")
    @classmethod
    def validate_ref(cls, v):
        try:
            Address(**v)
        except ValidationError as e:
            raise ValueError(e) from e
        return v

try:
    Order(ref={"postcode": "not-a-postcode", "street": ""})
except Exception as e:
    nice_errors = nested_error_to_nice(e)
    for err in nice_errors:
        print(f"{err.field}: {err.message}")
    # ref.postcode: String should match pattern '^\d{5}$'. Received type: str, value: 'not-a-postcode'
    # ref.street: Missing required field: street.

This will help turn your error from:

ValidationError: 1 validation error for Order
ref
  Value error, 2 validation errors for Address
  postcode
    String should match pattern '^\d{5}$' [type=string_pattern_mismatch, input_value='not-a-postcode', input_type=str]
      For further information visit https://errors.pydantic.dev/2.12/v/string_pattern_mismatch
  street
    Field required [type=missing, input_value={'postcode': 'not-a-postcode'}, input_type=dict]
      For further information visit https://errors.pydantic.dev/2.12/v/missing [type=value_error, ...]

To this:

ref.postcode: String should match pattern '^\d{5}$'. Received type: str, value: 'not-a-postcode'
ref.street: Missing required field: street.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_error_handling-0.1.2.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_error_handling-0.1.2-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_error_handling-0.1.2.tar.gz.

File metadata

  • Download URL: pydantic_error_handling-0.1.2.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pydantic_error_handling-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b6291b02e295ed7c03e16dc3d7ddb2408d9912a4ae3bac32c49c26912ea0892c
MD5 f790d9a6cad97c6a34142a928d30f2ba
BLAKE2b-256 bd945e5597f34659a7c944f190a7b933ad168ecc6b0e828d4111eee6ab2b45ab

See more details on using hashes here.

File details

Details for the file pydantic_error_handling-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_error_handling-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6970afdea70ee51a1fdef33aa56237b0ba60143f0fe9f424ad0322606ade9972
MD5 f94af903196ca217f9f2b9910d942aef
BLAKE2b-256 941b9fc632249d7db819cda754952ed0e3682bfe2236eb132db59a75e74e4216

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page