Spaces:
Sleeping

xzhou10 commited on
Commit
bc9ad31
·
1 Parent(s): a5730fa

fix: monkey-patch gradio_client get_type for bool additionalProperties

Browse files

show_api=False doesn't help because the schema is generated at app
import time (during get_blocks().get_api_info()) — before launch().
The crash:

File gradio_client/utils.py:863, in get_type
if 'const' in schema:
TypeError: argument of type 'bool' is not iterable

happens when a tool schema has additionalProperties: false (a bool,
not a dict). gradio 4.44 + bundled gradio_client 1.3.0 has this bug;
it's fixed in gradio_client 1.5.x but gradio 4.44 pins client to 1.3.

Workaround: patch get_type and _json_schema_to_python_type to fall
back to 'Any' when given a non-dict, before importing gradio. This
is a one-process fix that costs us nothing because we don't expose
a programmatic client to consumers.

Files changed (1) hide show
  1. app.py +33 -0
app.py CHANGED
@@ -33,6 +33,39 @@ import tempfile
33
  from pathlib import Path
34
  from typing import Any
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  import gradio as gr
37
  import pandas as pd
38
 
 
33
  from pathlib import Path
34
  from typing import Any
35
 
36
+ # ---------------------------------------------------------------------------
37
+ # Monkey-patch gradio_client.utils to fix
38
+ # "TypeError: argument of type 'bool' is not iterable"
39
+ # inside get_type when a JSON schema's `additionalProperties` is a bool
40
+ # instead of a dict. Affects gradio 4.44.0 + bundled gradio_client 1.3.0,
41
+ # triggered during app.api_info schema generation at startup. Must run
42
+ # BEFORE `import gradio` so the patched function is the one gradio sees.
43
+ # Upstream fix: gradio_client>=1.5.x. Until we upgrade gradio to 5.x,
44
+ # this in-process patch is the smallest safe workaround.
45
+ # ---------------------------------------------------------------------------
46
+ import gradio_client.utils as _gc_utils
47
+
48
+ _gc_orig_get_type = _gc_utils.get_type
49
+
50
+ def _gc_get_type_safe(schema):
51
+ if not isinstance(schema, dict):
52
+ return "Any"
53
+ return _gc_orig_get_type(schema)
54
+
55
+ _gc_utils.get_type = _gc_get_type_safe
56
+
57
+ _gc_orig_json_to_python = _gc_utils._json_schema_to_python_type
58
+
59
+ def _gc_json_to_python_safe(schema, defs=None):
60
+ if not isinstance(schema, dict):
61
+ return "Any"
62
+ try:
63
+ return _gc_orig_json_to_python(schema, defs)
64
+ except (TypeError, KeyError):
65
+ return "Any"
66
+
67
+ _gc_utils._json_schema_to_python_type = _gc_json_to_python_safe
68
+
69
  import gradio as gr
70
  import pandas as pd
71