Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gr.State serializes pydantic BaseModel objects at initialization #10021

Closed
1 task done
filiso opened this issue Nov 22, 2024 · 0 comments · Fixed by #10036
Closed
1 task done

gr.State serializes pydantic BaseModel objects at initialization #10021

filiso opened this issue Nov 22, 2024 · 0 comments · Fixed by #10036
Assignees
Labels
bug Something isn't working

Comments

@filiso
Copy link

filiso commented Nov 22, 2024

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? 🔎

  • I have searched and found no 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

@filiso filiso added the bug Something isn't working label Nov 22, 2024
@freddyaboulton freddyaboulton self-assigned this Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants