Repository: gradio-app/gradio
Pull request: Fix builtin functions for gr.State value
## Description

Small bug fix. Alternative is to not allow builtins at all.

Closes: #10653

## 🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is [an existing issue](https://github.com/gradio-app/gradio/issues) for this change. If not, please create an issue before you create this PR, unless the fix is very small. 

Not adhering to this guideline will result in the PR being closed. 

## Testing and Formatting Your Code

1. PRs will only be merged if tests pass on CI. We recommend at least running the backend tests locally, please set up [your Gradio environment locally](https://github.com/gradio-app/gradio/blob/main/CONTRIBUTING.md) and run the backed tests: `bash scripts/run_backend_tests.sh`

2. Please run these bash scripts to automatically format your code: `bash scripts/format_backend.sh`, and (if you made any changes to non-Python files) `bash scripts/format_frontend.sh`
  


Problem statement:
Setting gr.State value to a builtin causes error in inspection
### Describe the bug

Setting gr.State value to a dict or bool causes error in inspection. The workaround is to set the value to a function which returns a dict object.

This worked in previous versions of  gradio

### Have you searched existing issues?  🔎

- [x] I have searched and found no existing issues

### Reproduction

```python
import gradio as gr

with gr.Blocks() as app:
    gradio_graph_state = gr.State(
        dict
    )
app.launch()
```


### Screenshot

_No response_

### Logs

```shell
File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\site-packages\anyio\_backends\_asyncio.py", line 2461, in run_sync_in_worker_thread
    return await future
           ^^^^^^^^^^^^
  File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\site-packages\anyio\_backends\_asyncio.py", line 962, in run
    result = context.run(func, *args)
  File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\routes.py", line 553, in main
    gradio_api_info = api_info(request)
  File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\routes.py", line 583, in api_info
    api_info = utils.safe_deepcopy(app.get_blocks().get_api_info())
                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\blocks.py", line 2904, in get_api_info
    fn_info = utils.get_function_params(fn.fn)  # type: ignore
  File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\site-packages\gradio\utils.py", line 1310, in get_function_params
    signature = inspect.signature(func)
  File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\inspect.py", line 3344, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                   globals=globals, locals=locals, eval_str=eval_str)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\inspect.py", line 3070, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
                                    follow_wrapper_chains=follow_wrapped,
                                    globals=globals, locals=locals, eval_str=eval_str)
  File "C:\Users\Bryce\AppData\Local\Programs\Python\Python313\Lib\inspect.py", line 2642, in _signature_from_callable
    raise ValueError(
        'no signature found for builtin type {!r}'.format(obj))
ValueError: no signature found for builtin type <class 'dict'>
```

### System Info

```shell
Gradio Environment Information:
------------------------------
Operating System: Windows
gradio version: 5.17.1
gradio_client version: 1.7.1

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

aiofiles: 23.2.1
anyio: 4.8.0
audioop-lts is not installed.
fastapi: 0.115.8
ffmpy: 0.5.0
gradio-client==1.7.1 is not installed.
httpx: 0.28.1
huggingface-hub: 0.29.0
jinja2: 3.1.5
markupsafe: 2.1.5
numpy: 2.2.3
orjson: 3.10.15
packaging: 24.2
pandas: 2.2.3
pillow: 11.1.0
pydantic: 2.10.6
pydub: 0.25.1
python-multipart: 0.0.20
pyyaml: 6.0.2
ruff: 0.9.6
safehttpx: 0.1.6
semantic-version: 2.10.0
starlette: 0.45.3
tomlkit: 0.13.2
typer: 0.15.1
typing-extensions: 4.12.2
urllib3: 2.3.0
uvicorn: 0.34.0
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.


gradio_client dependencies in your environment:

fsspec: 2025.2.0
httpx: 0.28.1
huggingface-hub: 0.29.0
packaging: 24.2
typing-extensions: 4.12.2
websockets: 14.2
```

### Severity

I can work around it

Hints from issue discussion:

Commit message: Merge branch 'main' into fix-dict-callable

Patch to review:
diff --git a/.changeset/smooth-turkeys-notice.md b/.changeset/smooth-turkeys-notice.md
new file mode 100644
index 0000000000..2f76520b85
--- /dev/null
+++ b/.changeset/smooth-turkeys-notice.md
@@ -0,0 +1,5 @@
+---
+"gradio": patch
+---
+
+fix:Fix builtin functions for gr.State value
diff --git a/gradio/utils.py b/gradio/utils.py
index 37853d474c..0b0e76f0db 100644
--- a/gradio/utils.py
+++ b/gradio/utils.py
@@ -1308,7 +1308,10 @@ def get_function_params(func: Callable) -> list[tuple[str, bool, Any, Any]]:
     Excludes *args and **kwargs, as well as args that are Gradio-specific, such as gr.Request, gr.EventData, gr.OAuthProfile, and gr.OAuthToken.
     """
     params_info = []
-    signature = inspect.signature(func)
+    try:
+        signature = inspect.signature(func)
+    except Exception:
+        signature = inspect.Signature()
     type_hints = get_type_hints(func)
     for name, parameter in signature.parameters.items():
         if parameter.kind in (
diff --git a/test/test_blocks.py b/test/test_blocks.py
index e7eca6f4c8..358fcc6897 100644
--- a/test/test_blocks.py
+++ b/test/test_blocks.py
@@ -529,6 +529,11 @@ def test_get_load_events(self, io_components):
             comp.load_event in demo.config["dependencies"] for comp in components
         )
 
+    def test_load_events_work_with_builtins(self):
+        with gr.Blocks() as demo:
+            gr.State(dict)
+        demo.get_api_info()
+
 
 class TestBlocksPostprocessing:
     @pytest.mark.asyncio


Code review comments:
diff_hunk: @@ -1308,7 +1308,10 @@ def get_function_params(func: Callable) -> list[tuple[str, bool, Any, Any]]:
     Excludes *args and **kwargs, as well as args that are Gradio-specific, such as gr.Request, gr.EventData, gr.OAuthProfile, and gr.OAuthToken.
     """
     params_info = []
-    signature = inspect.signature(func)
+    try:
+        signature = inspect.signature(func)
+    except Exception:
path: gradio/utils.py
text: @user1:
I think we can be more specific:
```suggestion
    except ValueError:
```