Convert JS objects / JsProxy / mappings into a real Python dict.
(args: Any)
| 17 | |
| 18 | |
| 19 | def _coerce_args(args: Any) -> dict[str, Any]: |
| 20 | """Convert JS objects / JsProxy / mappings into a real Python dict.""" |
| 21 | if args is None: |
| 22 | return {} |
| 23 | |
| 24 | # Pyodide: JS object arrives as JsProxy; convert recursively to Python. |
| 25 | if JsProxy is not None and isinstance(args, JsProxy): |
| 26 | # dict_converter=dict ensures JS object -> Python dict (not Map) |
| 27 | return to_py(args, dict_converter=dict) |
| 28 | |
| 29 | # Already a Python dict |
| 30 | if isinstance(args, dict): |
| 31 | return args |
| 32 | |
| 33 | # Any Mapping-like object |
| 34 | if isinstance(args, Mapping): |
| 35 | return dict(args) |
| 36 | |
| 37 | # Last resort: try dict() coercion |
| 38 | try: |
| 39 | return dict(args) |
| 40 | except Exception as e: |
| 41 | raise TypeError(f"Tool args must be a mapping/dict; got {type(args)}") from e |
| 42 | |
| 43 | |
| 44 | def tools_openai() -> list[dict[str, Any]]: |