# patch_gradio_jsonschema.py """ Robust runtime monkeypatch for gradio_client JSON Schema handling. Import this BEFORE importing gradio or gradio_client. This version uses *args/**kwargs wrappers to be compatible with varying signatures and avoids merge-conflict artefacts. """ try: import gradio_client.utils as _gc_utils except Exception: _gc_utils = None def _is_bool_schema(x): return isinstance(x, bool) def _bool_to_safe_schema(b: bool): if b: return {} else: return {"type": "___boolean_schema_false___"} def _wrap_function_coercing_schema(orig): """ Wraps functions that accept a schema as first positional arg. Coerces boolean schemas into safe dicts before calling the original. Works for functions with signature like f(schema), f(schema, defs), etc. """ def wrapper(*args, **kwargs): # If first positional arg is a boolean schema, replace it if len(args) >= 1 and _is_bool_schema(args[0]): new_first = _bool_to_safe_schema(args[0]) new_args = (new_first,) + args[1:] return orig(*new_args, **kwargs) # If schema passed as keyword arg if "schema" in kwargs and _is_bool_schema(kwargs["schema"]): kw = dict(kwargs) kw["schema"] = _bool_to_safe_schema(kwargs["schema"]) return orig(*args, **kw) return orig(*args, **kwargs) try: wrapper.__name__ = getattr(orig, "__name__", "wrapped") wrapper.__doc__ = getattr(orig, "__doc__", "") except Exception: pass return wrapper def apply_patch(): global _gc_utils if _gc_utils is None: try: import gradio_client.utils as _gc_utils except Exception: return False # Patch get_type if present if hasattr(_gc_utils, "get_type"): # preserve original if not already saved if not hasattr(_gc_utils, "_orig_get_type"): _gc_utils._orig_get_type = _gc_utils.get_type _gc_utils.get_type = _wrap_function_coercing_schema(_gc_utils.get_type) # Patch internal _json_schema_to_python_type if present if hasattr(_gc_utils, "_json_schema_to_python_type"): if not hasattr(_gc_utils, "_orig__json_schema_to_python_type"): _gc_utils._orig__json_schema_to_python_type = _gc_utils._json_schema_to_python_type _gc_utils._json_schema_to_python_type = _wrap_function_coercing_schema( _gc_utils._json_schema_to_python_type ) # Patch public helper json_schema_to_python_type if present if hasattr(_gc_utils, "json_schema_to_python_type"): if not hasattr(_gc_utils, "_orig_json_schema_to_python_type"): _gc_utils._orig_json_schema_to_python_type = _gc_utils.json_schema_to_python_type _gc_utils.json_schema_to_python_type = _wrap_function_coercing_schema( _gc_utils.json_schema_to_python_type ) return True # apply at import time apply_patch() # small diagnostic print visible in runtime logs try: print("patch_gradio_jsonschema applied (clean)") except Exception: pass