Fix Pydantic v2 Schema Error for Image Field in /api/generate - #29
Open
jumasheff wants to merge 1 commit into
Open
Fix Pydantic v2 Schema Error for Image Field in /api/generate#29jumasheff wants to merge 1 commit into
jumasheff wants to merge 1 commit into
Conversation
Owner
|
Thanks for the PR. If ndarray cannot be used in types, does that mean osam/osam/types/_image_embedding.py Lines 9 to 15 in f361175 |
wkentaro
force-pushed
the
main
branch
3 times, most recently
from
January 11, 2026 02:53
a130e25 to
e32b669
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves a pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'numpy.ndarray'> that occurred during FastAPI startup/request validation for the /api/generate endpoint.
Problem:
The previous implementation used
Optional[np.ndarray]as the type hint for theimagefield in theGenerateRequest Pydanticmodel and employed a@field_validator(..., mode="before")to convert an incoming Base64 string to a NumPy array before validation. Pydantic v2 struggles to generate a schema directly fornp.ndarray, leading to the error even witharbitrary_types_allowed=True.Solution:
GenerateRequestModel:request.model_copy(update={"image": converted_array})to pass the correctly typed NumPy array to thebackend apis.generatefunction.This approach ensures Pydantic works with standard types during schema generation and validation, deferring the specialized NumPy array conversion to the endpoint handler logic, thus fixing the startup error.