@@ -74,6 +74,25 @@ def emit_warning(self, kind: JsonSchemaWarningKind, detail: str) -> None:
7474 raise ValueError (f"JSON schema warning: { kind } - { detail } " )
7575
7676
77+ _LOCAL_DEFS_PREFIX = "#/$defs/"
78+
79+
80+ def _inline_root_ref (schema : dict [str , Any ]) -> dict [str , Any ]:
81+ """Give a schema whose root is a bare `$ref` into `$defs` an inline root.
82+
83+ pydantic emits a self-referential model as `{"$defs": {...}, "$ref": "#/$defs/Model"}`, with no
84+ `type` at the root; `Tool.outputSchema` needs an object root (required on the wire through
85+ 2025-11-25). The referenced definition is copied onto the root and `$defs` is kept, since nested
86+ references still point into it. Root siblings of the `$ref` win over the definition's keys.
87+ """
88+ ref = schema .get ("$ref" )
89+ if not isinstance (ref , str ) or not ref .startswith (_LOCAL_DEFS_PREFIX ):
90+ return schema
91+ definition = cast (dict [str , Any ], schema ["$defs" ][ref .removeprefix (_LOCAL_DEFS_PREFIX )])
92+ siblings = {key : value for key , value in schema .items () if key != "$ref" }
93+ return {** definition , ** siblings }
94+
95+
7796class ArgModelBase (BaseModel ):
7897 """A model representing the arguments to a function."""
7998
@@ -108,7 +127,8 @@ class FuncMetadata(BaseModel):
108127 def model_post_init (self , context : Any , / ) -> None :
109128 if self .output_model is not None and self .output_schema is None :
110129 # StrictJsonSchema raises instead of warning, so an unserializable return type fails construction.
111- self .output_schema = self ._output_adapter (self .output_model ).json_schema (schema_generator = StrictJsonSchema )
130+ schema = self ._output_adapter (self .output_model ).json_schema (schema_generator = StrictJsonSchema )
131+ self .output_schema = _inline_root_ref (schema )
112132
113133 def _output_adapter (self , output_model : type [Any ]) -> TypeAdapter [Any ]:
114134 """The validator/serializer for `output_model`, built once and rebuilt only if the field is reassigned."""
0 commit comments