Skip to content

Fix Pydantic v2 Schema Error for Image Field in /api/generate - #29

Open
jumasheff wants to merge 1 commit into
wkentaro:mainfrom
jumasheff:main
Open

Fix Pydantic v2 Schema Error for Image Field in /api/generate#29
jumasheff wants to merge 1 commit into
wkentaro:mainfrom
jumasheff:main

Conversation

@jumasheff

@jumasheff jumasheff commented Apr 22, 2025

Copy link
Copy Markdown

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 the image field in the GenerateRequest Pydantic model 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 for np.ndarray, leading to the error even with arbitrary_types_allowed=True.

Solution:

  1. Modified GenerateRequest Model:
  • Changed the type hint for the image field from Optional[np.ndarray] to Optional[str].
  • Removed the @field_validator for the image field.
  1. Updated Endpoint Logic:
  • The /api/generate endpoint now expects request.image as an Optional[str], allowing Pydantic to validate it successfully.
  • The conversion from a Base64 string to a np.ndarray is now handled explicitly within the endpoint after the initial request validation.
  • Error handling for invalid Base64 data during conversion was added.
  • Used request.model_copy(update={"image": converted_array}) to pass the correctly typed NumPy array to the backend apis.generate function.

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.

@wkentaro

wkentaro commented Apr 23, 2025

Copy link
Copy Markdown
Owner

Thanks for the PR.

If ndarray cannot be used in types, does that mean ImageEmbedding also has to change?

class ImageEmbedding(pydantic.BaseModel):
model_config = pydantic.ConfigDict(arbitrary_types_allowed=True)
original_height: int
original_width: int
embedding: npt.NDArray[np.float32]
extra_features: list[npt.NDArray[np.float32]] = pydantic.Field(default_factory=list)

@wkentaro
wkentaro force-pushed the main branch 3 times, most recently from a130e25 to e32b669 Compare January 11, 2026 02:53
@wkentaro wkentaro added the needs-triage issue: Maintainer needs to evaluate this issue label Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-triage issue: Maintainer needs to evaluate this issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants