Skip to content

Fix issue #1160: Handle None value for by_alias in model_dump#1171

Closed
veeceey wants to merge 1 commit intoanthropics:mainfrom
veeceey:fix/issue-1160-pydantic-by-alias
Closed

Fix issue #1160: Handle None value for by_alias in model_dump#1171
veeceey wants to merge 1 commit intoanthropics:mainfrom
veeceey:fix/issue-1160-pydantic-by-alias

Conversation

@veeceey
Copy link
Copy Markdown

@veeceey veeceey commented Feb 8, 2026

Summary

Fixes TypeError when by_alias=None is passed to model_dump() in Pydantic v2 context.

When the Anthropic SDK is used in a FastAPI application with Pydantic v2, the model_dump() function in _compat.py was passing by_alias=None directly to pydantic's model_dump(), which requires a boolean value. This caused a TypeError in Pydantic-Core.

Changes

  • Updated model_dump() to default by_alias to False when None is passed
  • Added test cases to verify the fix works with by_alias=None, True, and False

Related Issues

Fixes #1160

…n model_dump

When by_alias is None, Pydantic v2's model_dump() requires a boolean value
but was receiving None, causing TypeError. Now defaults to False when None.
@veeceey veeceey requested a review from a team as a code owner February 8, 2026 09:47
@veeceey
Copy link
Copy Markdown
Author

veeceey commented Feb 8, 2026

Closing as duplicate of #1168 which was opened earlier with the same fix.

@veeceey veeceey closed this Feb 8, 2026
@webshark
Copy link
Copy Markdown

Has this been released yet as a fix in the main ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool' when using SDK in FastAPI context

2 participants