gradio-app / gradio

Build and share delightful machine learning apps, all in Python. šŸŒŸ Star to support our work!
http://www.gradio.app
Apache License 2.0
34.11k stars 2.59k forks source link

gr.State serializes pydantic BaseModel objects at initialization #10021

Open filiso opened 5 hours ago

filiso commented 5 hours ago

Describe the bug

When passing an object based on the Pydantic BaseModel to gr.State during initalization of the gr.State the object gets serialized into a dictionary.

This doesn't happen for a regular class object.

Interestingly, when passing a Pydantic object into an already initialized gr.State the serialization does not occur (at least in a certain scenario).

Have you searched existing issues? šŸ”Ž

Reproduction

import gradio as gr
from pydantic import BaseModel

class TestRegular:
    def __init__(self, name):
        self.name = name

class TestPydantic(BaseModel):
    name: str

t_reg = TestRegular("hey there")
t_pyd = TestPydantic(name="wassup")

state_reg = gr.State(t_reg)
state_pyd = gr.State(t_pyd)

print("== regular class remains unchanged ==")
print(f"{t_reg=}")
print(f"{state_reg.value=}")

print("== pydantic is serialized ==")
print(f"{t_pyd=}")
print(f"{state_pyd.value=}")

Screenshot

image The output of running the above code snippet.

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 5.6.0
gradio_client version: 1.4.3

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
anyio: 4.4.0
audioop-lts is not installed.
fastapi: 0.115.5
ffmpy: 0.4.0
gradio-client==1.4.3 is not installed.
httpx: 0.27.2
huggingface-hub: 0.25.2
jinja2: 3.1.4
markupsafe: 2.1.3
numpy: 1.26.4
orjson: 3.10.7
packaging: 24.1
pandas: 2.2.2
pillow: 10.4.0
pydantic: 2.10.1
pydub: 0.25.1
python-multipart==0.0.12 is not installed.
pyyaml: 6.0.2
ruff: 0.6.7
safehttpx: 0.1.1
semantic-version: 2.10.0
starlette: 0.41.3
tomlkit==0.12.0 is not installed.
typer: 0.12.5
typing-extensions: 4.12.2
urllib3: 2.2.2
uvicorn: 0.30.6
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2024.5.0
httpx: 0.27.2
huggingface-hub: 0.25.2
packaging: 24.1
typing-extensions: 4.12.2
websockets: 12.0

Severity

I can work around it