Spaces:
Sleeping
Sleeping
ibcplateformes Claude Opus 4.6 commited on
Commit ·
c1ed66c
1
Parent(s): 888e081
Fix gradio 4.44.0 crash: catch TypeError instead of modifying schema dicts
Browse filesThe previous monkey-patch modified schema dicts (deleting additionalProperties),
which leaked into jinja2's LRU cache causing "unhashable type: dict" errors.
New approach: wrap get_type and _json_schema_to_python_type with try/except
TypeError, plus add safety net on Blocks.get_api_info.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -11,10 +11,13 @@ import shutil
|
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
|
| 14 |
-
# ── Monkey-patch
|
| 15 |
-
#
|
| 16 |
-
#
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
import gradio_client.utils as _gc_utils
|
| 20 |
|
|
@@ -23,7 +26,10 @@ try:
|
|
| 23 |
def _patched_get_type(schema):
|
| 24 |
if isinstance(schema, bool):
|
| 25 |
return "Any"
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
_gc_utils.get_type = _patched_get_type
|
| 29 |
|
|
@@ -32,17 +38,30 @@ try:
|
|
| 32 |
def _patched_json_schema(schema, defs=None):
|
| 33 |
if isinstance(schema, bool):
|
| 34 |
return "Any"
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
del schema["additionalProperties"]
|
| 40 |
-
return _original_json_schema(schema, defs)
|
| 41 |
|
| 42 |
_gc_utils._json_schema_to_python_type = _patched_json_schema
|
| 43 |
except Exception:
|
| 44 |
pass
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Setup logging
|
| 47 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 48 |
logger = logging.getLogger(__name__)
|
|
|
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
|
| 14 |
+
# ── Monkey-patch gradio 4.44.0 bugs ─────────────────────────────────────────
|
| 15 |
+
# Bug 1: gradio_client/utils.py get_type() crashes with TypeError when
|
| 16 |
+
# schema is a bool (additionalProperties: true in JSON schema).
|
| 17 |
+
# Bug 2: _json_schema_to_python_type() also crashes on bool schemas.
|
| 18 |
+
# Strategy: Wrap both functions to catch TypeError — do NOT modify any schema
|
| 19 |
+
# dicts, as that leaks into jinja2's LRU cache and causes
|
| 20 |
+
# "unhashable type: dict" errors in template rendering.
|
| 21 |
try:
|
| 22 |
import gradio_client.utils as _gc_utils
|
| 23 |
|
|
|
|
| 26 |
def _patched_get_type(schema):
|
| 27 |
if isinstance(schema, bool):
|
| 28 |
return "Any"
|
| 29 |
+
try:
|
| 30 |
+
return _original_get_type(schema)
|
| 31 |
+
except TypeError:
|
| 32 |
+
return "Any"
|
| 33 |
|
| 34 |
_gc_utils.get_type = _patched_get_type
|
| 35 |
|
|
|
|
| 38 |
def _patched_json_schema(schema, defs=None):
|
| 39 |
if isinstance(schema, bool):
|
| 40 |
return "Any"
|
| 41 |
+
try:
|
| 42 |
+
return _original_json_schema(schema, defs)
|
| 43 |
+
except TypeError:
|
| 44 |
+
return "Any"
|
|
|
|
|
|
|
| 45 |
|
| 46 |
_gc_utils._json_schema_to_python_type = _patched_json_schema
|
| 47 |
except Exception:
|
| 48 |
pass
|
| 49 |
|
| 50 |
+
# Safety net: patch Blocks.get_api_info to prevent any residual TypeError
|
| 51 |
+
# from crashing the entire app during health checks or page loads.
|
| 52 |
+
try:
|
| 53 |
+
_orig_get_api_info = gr.Blocks.get_api_info
|
| 54 |
+
|
| 55 |
+
def _safe_get_api_info(self, *args, **kwargs):
|
| 56 |
+
try:
|
| 57 |
+
return _orig_get_api_info(self, *args, **kwargs)
|
| 58 |
+
except TypeError:
|
| 59 |
+
return {"named_endpoints": {}, "unnamed_endpoints": {}}
|
| 60 |
+
|
| 61 |
+
gr.Blocks.get_api_info = _safe_get_api_info
|
| 62 |
+
except Exception:
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
# Setup logging
|
| 66 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 67 |
logger = logging.getLogger(__name__)
|