Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@s21v1d9p
Copy link

Description

This PR adds __str__ methods to RequestValidationError and WebSocketRequestValidationError to provide
human-readable string representations, improving consistency with ResponseValidationError which already has this
functionality.

Motivation

Currently, when debugging validation errors:

  • ResponseValidationError has a helpful __str__ method that formats errors clearly
  • RequestValidationError and WebSocketRequestValidationError fall back to the default Python object
    representation
  • This inconsistency makes debugging request validation errors harder than necessary

Changes

  • ✅ Added __str__ method to RequestValidationError
  • ✅ Added __str__ method to WebSocketRequestValidationError
  • ✅ Added comprehensive tests in tests/test_validation_error_str.py

Example Output

Before:

<fastapi.exceptions.RequestValidationError object at 0x7f8b8c0a3d90>

After:
2 validation errors for request:
  {'type': 'int_parsing', 'loc': ['query', 'id'], 'msg': 'Input should be a valid integer'}
  {'type': 'missing', 'loc': ['query', 'name'], 'msg': 'Field required'}

Benefits

- Better debugging experience - Error messages are immediately readable in logs and console output
- Consistency - All validation error classes now have the same string representation behavior
- Non-breaking - Existing code continues to work; this only enhances the developer experience when errors are
converted to strings

Testing

All existing tests pass, and new tests verify:
- Single error formatting
- Multiple error formatting
- Proper pluralization ("1 validation error" vs "2 validation errors")
- Consistency across all validation error types

Checklist

- Tests added for the new functionality
- All tests pass
- Code follows existing style and patterns
- Non-breaking change

s21v1d9p and others added 2 commits November 28, 2025 16:24
Added human-readable string representations to RequestValidationError and
WebSocketRequestValidationError classes, consistent with the existing
ResponseValidationError implementation. This improves developer experience
when debugging validation errors by providing clear, formatted error messages.

Changes:
- Add __str__ method to RequestValidationError
- Add __str__ method to WebSocketRequestValidationError
- Add comprehensive tests for the new string representations

The string format shows the number of errors and lists each error on a
separate line, making it easier to read error messages during debugging
and logging.
@YuriiMotov
Copy link
Member

Duplicate of: #12972

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Dec 6, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 6, 2025

This pull request has a merge conflict that needs to be resolved.

@tiangolo
Copy link
Member

This was included in a recent PR, so we can close it now. Thanks! ☕

@tiangolo tiangolo closed this Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants