gradio: Pydantic-core 2.23.2 is breaking gradio
Describe the bug
Pydantic-core dropped 2.23.2 today and there seems to be a breaking change.
Now, gradio gives me this monster of a traceback.
Falling back to Pydanticc-core 2.22.0 seems to resolve the issue, but I’ll test some other verisons and report back.
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/pydantic/type_adapter.py", line 279, in _init_core_attrs
self._core_schema = _getattr_no_parents(self._type, '__pydantic_core_schema__')
File "/usr/local/lib/python3.10/site-packages/pydantic/type_adapter.py", line 121, in _getattr_no_parents
raise AttributeError(attribute)
AttributeError: __pydantic_core_schema__
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 406, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 70, in __call__
return await self.app(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/fastapi/applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 113, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 187, in __call__
raise exc
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 165, in __call__
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.10/site-packages/gradio/route_utils.py", line 766, in __call__
await self.simple_response(scope, receive, send, request_headers=headers)
File "/usr/local/lib/python3.10/site-packages/gradio/route_utils.py", line 782, in simple_response
await self.app(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 62, in wrapped_app
raise exc
File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 51, in wrapped_app
await app(scope, receive, sender)
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 715, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 735, in app
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 288, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 76, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 62, in wrapped_app
raise exc
File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 51, in wrapped_app
await app(scope, receive, sender)
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 73, in app
response = await f(request)
File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 291, in app
solved_result = await solve_dependencies(
File "/usr/local/lib/python3.10/site-packages/fastapi/dependencies/utils.py", line 639, in solve_dependencies
) = await request_body_to_args( # body_params checked above
File "/usr/local/lib/python3.10/site-packages/fastapi/dependencies/utils.py", line 810, in request_body_to_args
fields_to_extract = get_model_fields(first_field.type_)
File "/usr/local/lib/python3.10/site-packages/fastapi/_compat.py", line 283, in get_model_fields
return [
File "/usr/local/lib/python3.10/site-packages/fastapi/_compat.py", line 284, in <listcomp>
ModelField(field_info=field_info, name=name)
File "<string>", line 6, in __init__
File "/usr/local/lib/python3.10/site-packages/fastapi/_compat.py", line 109, in __post_init__
self._type_adapter: TypeAdapter[Any] = TypeAdapter(
File "/usr/local/lib/python3.10/site-packages/pydantic/type_adapter.py", line 266, in __init__
self._init_core_attrs(rebuild_mocks=False)
File "/usr/local/lib/python3.10/site-packages/pydantic/type_adapter.py", line 144, in wrapped
return func(self, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/pydantic/type_adapter.py", line 286, in _init_core_attrs
self._core_schema = _get_schema(self._type, config_wrapper, parent_depth=self._parent_depth)
File "/usr/local/lib/python3.10/site-packages/pydantic/type_adapter.py", line 104, in _get_schema
schema = gen.generate_schema(type_)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 655, in generate_schema
schema = self._generate_schema_inner(obj)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 908, in _generate_schema_inner
return self._annotated_schema(obj)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 2025, in _annotated_schema
schema = self._apply_annotations(source_type, annotations)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 2104, in _apply_annotations
schema = get_inner_schema(source_type)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 83, in __call__
schema = self._handler(source_type)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 2186, in new_handler
schema = metadata_get_schema(source, get_inner_schema)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 2182, in <lambda>
lambda source, handler: handler(source)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 83, in __call__
schema = self._handler(source_type)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 2085, in inner_handler
schema = self._generate_schema_inner(obj)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 929, in _generate_schema_inner
return self.match_type(obj)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1029, in match_type
return self._match_generic_type(obj, origin)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1058, in _match_generic_type
return self._union_schema(obj)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1378, in _union_schema
choices.append(self.generate_schema(arg))
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 655, in generate_schema
schema = self._generate_schema_inner(obj)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 929, in _generate_schema_inner
return self.match_type(obj)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1038, in match_type
return self._unknown_type_schema(obj)
File "/usr/local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 558, in _unknown_type_schema
raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'starlette.requests.Request'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.
If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.
For further information visit https://errors.pydantic.dev/2.9/u/schema-for-unknown-type
^CKeyboard interruption in main thread... closing server.
Have you searched existing issues? 🔎
- I have searched and found no existing issues
Reproduction
cd /tmp
cat <<EOM > test.py
import gradio as gr
def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)
demo = gr.Interface(
fn=greet,
inputs=["text", "slider"],
outputs=["text"],
)
demo.launch(server_name="0.0.0.0")
EOM
python3 test.py
Screenshot
No response
Logs
No response
System Info
Python 3.10 Docker container + `pip install gradio`
Severity
I can work around it
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 18 (1 by maintainers)
Commits related to this issue
- Updated gradio version for Pydantic-core bug fix https://github.com/gradio-app/gradio/issues/9278 — committed to iVoider/Applio by iVoider 2 years ago
- fix: Inconsistencies between FastAPI and Pydantic #85, #86 Refers to https://github.com/gradio-app/gradio/issues/9278#issuecomment-2336919276 — committed to R3gm/SoniTranslate by R3gm 2 years ago
I was using
gradio==4.29.0and had the same problem.Updating gradio to 4.43.0 solved the problem.
*It seems this related with fastapi
All other dependencies except for
gradioare automatically installed when installinggradio.I think I found the bug. It’s here: https://github.com/gradio-app/gradio/blob/08b51590163b306fd874f543f6fcaf23ac7d2646/gradio/data_classes.py#L62
Gradio is putting the FastAPI
Requestinside of aPredictBodyPydantic model, which would be used to validate and handle the body data, so theRequestshouldn’t really be there.The fact that it worked before was more coincidence than anything, but that is not supported, documented, or recommended. 😅 😬
The
Requestshould be handled outside of that model.I see two approaches you can use to handle it.
Approach 1
Separate the
Requestfrom thePredictBodyPydantic model, that’s what I would recommend.Anything that was receiving a parameter
body: PredictBodyand reading the request object frombody.requestshould now receive arequest: fastapi.Requestparameter directly and use that. Or it could be in some other object, but not the Pydantic model used to validate API data.In fact, some of the functions using that Pydantic model already receive a
Requestparameter.Approach 2
You could refactor
PredictBody(and probably rename it) to be a dependency class, it would take in its__init__abody: PredictBodyand arequest: Request.And then refactor the code that uses that object to access
obj.bodyandobj.requestto get each of the two objects.I think this option is more cumbersome and again is coupling two things that shouldn’t be together, those are two different levels of abstraction, one is used for data validation, serialization, data filtering, documentation, etc. The other one is the low-level raw HTTP abstraction object.
a temporary solution worked for me is downgrading fastapi version
pip install fastapi==0.112.2Ah interesting, we are in the process of releasing a new version of
gradiopinning to an older version offastapi, (see https://github.com/gradio-app/gradio/pull/9277) but it may be the issue is actually withpydantic-core. Please let us know what you find!stop it, Furkan
@freddyaboulton
In previous releases FastAPI would not use the Pydantic model declared for a body request directly, it would extract the fields and use each field. Now, when there’s a single model, it can use it directly to parse the data purely in Pydantic. This means better performance, correctness, etc.
I think reason why it would have worked before is because it would take the field declared as having a
Requestas an independent field and put the request there. But now that it passes the body content directly to the Pydantic model for it to parse it, there’s no (accidental) logic also putting theRequestthere.This also enabled a feature of supporting the declaration of form fields in a single Pydantic model. It will also enable declaring Query, Header, and Cookie parameters in a single Pydantic model, but shhh 🤫, that’s a surprise feature for next week.
thx, this works for me
It’s not just pydantic-core imo. I only upgraded pydantic and kept fastapi the same and gradio works. It’s something about the new starlette version and pydantic-core I think.
But I have to downgrade to
4.41.0to solve this issueImage.selectcan not be triggered. What a sad news. https://github.com/gradio-app/gradio/issues/9182Thanks for the detailed response @tiangolo ! I can refactor to use approach 1. Out of curiosity, what changed in starlette/fastapi that triggers this exception?