Preserve nested model types in prediction requests

This commit is contained in:
Andras Schmelczer 2026-10-27 16:51:58 +00:00
commit ca41feb328

View file

@ -20,13 +20,14 @@ def bootstrap_prediction_endpoint(
@router.post("/predict", status_code=status.HTTP_200_OK, response_model=Trace)
async def predict(input_value: schema) -> Trace: # type: ignore
try:
# model_dump() recursively converts nested models to dictionaries,
# losing the validated argument types expected by the function.
arguments = {
name: getattr(input_value, name) for name in schema.model_fields
}
if inspect.iscoroutinefunction(func):
return await cast(Callable[..., Awaitable[Trace]], func)(
**cast(BaseModel, input_value).model_dump()
)
return cast(Callable[..., Trace], func)(
**cast(BaseModel, input_value).model_dump()
)
return await cast(Callable[..., Awaitable[Trace]], func)(**arguments)
return cast(Callable[..., Trace], func)(**arguments)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,