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

Skip to content

🐛 Remove Required shadowing from fastapi using Pydantic v2#12197

Merged
tiangolo merged 10 commits into
fastapi:masterfrom
pachewise:fix/required-shadowing
Oct 12, 2024
Merged

🐛 Remove Required shadowing from fastapi using Pydantic v2#12197
tiangolo merged 10 commits into
fastapi:masterfrom
pachewise:fix/required-shadowing

Conversation

@pachewise

@pachewise pachewise commented Sep 13, 2024

Copy link
Copy Markdown
Contributor

Re:

(and potentially others)

I think I found the underlying cause. We're still supporting pydantic.Required in FastAPI, which has the possibility to shadow the new Required type. To me, this seems like a Pydantic + fastapi bug. Here's how 0.113.0 introduced it:

  1. fastapi has the new get_model_fields() function, which deals with TypeAdapter(), and is called for every field
  2. TypeAdapter gets a globalns from the sys._getframe(), meaning it gets the ns from _compat. This is important because pydantic bases their type evaluations from that globalns. Why they are doing this is a question for later.
  3. Because Required = PydanticUndefined is in _compat.py, we are getting that value set in the globalns
  4. Independently, openai uses TypedDict with the Required annotations in its fields.
  5. vllm (and potentially other libs) uses fastapi + pydantic + openai TypedDict classes

The smoking gun was: TypeError: 'pydantic_core._pydantic_core.PydanticUndefinedType' object is not subscriptable

Focusing on vllm-project/vllm#8450, the offending type annotation was Required[Union[str, Iterable[X]]. It needed to have been Required that was a PydanticUndefinedType, otherwise it would be complaining about Union[str, Iterable[X]], or some other annotation. I ran the snippet here using fastapi 0.114.1, pydantic 2.9.0, vllm 0.6.1, python = 3.8.20, and was able to reproduce the issue again. Using pdb.pm(), I was able to inspect the globalns and connect the dots.

How to test

Run the code example in this comment Python 3.8. It will fail without the changes, and pass with the changes.

Comment thread fastapi/_compat.py Outdated

@app.get("/items/")
async def read_items(q: Annotated[str, Query(min_length=3)] = Required):
async def read_items(q: Annotated[str, Query(min_length=3)] = ...):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These examples seemed old, and I don't see them in the published docs. Guidance wanted on whether these need to be updated, deleted, or left untouched.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been deleted from the (English) docs in PR #10469, but the Russian documentation does in fact still reference it in docs/ru/tutorial/query-params-str-validations.md. Ideally, we probably should remove the reference from the Russian docs as well, and then remove this file altogether, but I would suggest to do that in a separate PR to avoid mixing up different issues - it's always easier to review atomic PRs.

@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit cc0ef63 at: https://db1b6e45.fastapitiangolo.pages.dev

@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit b55c86b at: https://3a774e90.fastapitiangolo.pages.dev

@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit 82addc4 at: https://f985d679.fastapitiangolo.pages.dev

@pachewise pachewise force-pushed the fix/required-shadowing branch from ba2b2f2 to a651737 Compare September 13, 2024 13:55
@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit a651737 at: https://96a7c93a.fastapitiangolo.pages.dev

@pachewise pachewise force-pushed the fix/required-shadowing branch from a651737 to f619617 Compare September 16, 2024 00:29
@pachewise pachewise marked this pull request as ready for review September 16, 2024 00:30
@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit f619617 at: https://d78181ad.fastapitiangolo.pages.dev

@svlandeg svlandeg added the bug Something isn't working label Sep 16, 2024
@svlandeg svlandeg changed the title fix: Remove Required shadowing from fastapi using Pydantic v2 🐛 Remove Required shadowing from fastapi using Pydantic v2 Sep 16, 2024
@svlandeg

Copy link
Copy Markdown
Member

Thanks for the PR and the detailed description and links! It looks like you've done some great digging 🙏
We'll get this reviewed by the team in more detail and will update here once we do.

@svlandeg svlandeg self-assigned this Sep 16, 2024

@svlandeg svlandeg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for the digging! The proposed fix here makes sense to me and avoids further confusion with Required. I only had a few minor nitpicking comments. I'll ask Tiangolo to review as well 🙏

Comment thread fastapi/_compat.py Outdated
Comment thread fastapi/_compat.py Outdated
@svlandeg svlandeg removed their assignment Sep 16, 2024
@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit f68466c at: https://c664766a.fastapitiangolo.pages.dev

@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit b6b2921 at: https://86077f7f.fastapitiangolo.pages.dev

@pachewise pachewise force-pushed the fix/required-shadowing branch from b6b2921 to d7b0eb2 Compare September 17, 2024 12:46
@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit d7b0eb2 at: https://d5ffc61e.fastapitiangolo.pages.dev

@MoritzLaurer

Copy link
Copy Markdown

Will this be merged soon? I'm encountering a related issue with vLLM

@tiangolo

Copy link
Copy Markdown
Member

Great job with all the detective work @pachewise! And thanks for the very clear explanation and references.

Thanks a lot @svlandeg for all your help and review! 🙇

This will be available in the next hours in FastAPI 0.115.1 🚀

@tiangolo tiangolo merged commit 8ae4603 into fastapi:master Oct 12, 2024
@pachewise pachewise deleted the fix/required-shadowing branch October 15, 2024 17:37
s-rigaud pushed a commit to s-rigaud/fastapi that referenced this pull request Jan 23, 2025
…#12197)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sofie Van Landeghem <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants