@@ -104,6 +104,49 @@ public void AsOpenAIChatTool_ProducesValidInstance()
104104 ValidateSchemaParameters ( tool . FunctionParameters ) ;
105105 }
106106
107+ [ Fact ]
108+ public void AsOpenAIChatTool_PreservesExtraTopLevelPropertiesLikeDefs ( )
109+ {
110+ // Create a JSON schema with $defs (used for reference types)
111+ var jsonSchema = JsonDocument . Parse ( """
112+ {
113+ "type": "object",
114+ "properties": {
115+ "person": { "$ref": "#/$defs/Person" }
116+ },
117+ "required": ["person"],
118+ "$defs": {
119+ "Person": {
120+ "type": "object",
121+ "properties": {
122+ "name": { "type": "string" }
123+ }
124+ }
125+ }
126+ }
127+ """ ) . RootElement ;
128+
129+ var functionWithDefs = AIFunctionFactory . CreateDeclaration (
130+ "test_function_with_defs" ,
131+ "A test function with $defs" ,
132+ jsonSchema ) ;
133+
134+ var tool = functionWithDefs . AsOpenAIChatTool ( ) ;
135+
136+ Assert . NotNull ( tool ) ;
137+ Assert . Equal ( "test_function_with_defs" , tool . FunctionName ) ;
138+ Assert . Equal ( "A test function with $defs" , tool . FunctionDescription ) ;
139+
140+ // Verify that $defs is preserved in the function parameters
141+ using var parsedParams = JsonDocument . Parse ( tool . FunctionParameters ) ;
142+ var root = parsedParams . RootElement ;
143+
144+ Assert . True ( root . TryGetProperty ( "$defs" , out var defs ) , "The $defs property should be preserved in the function parameters" ) ;
145+ Assert . True ( defs . TryGetProperty ( "Person" , out var person ) , "The Person definition should exist in $defs" ) ;
146+ Assert . True ( person . TryGetProperty ( "properties" , out var properties ) , "Person should have properties" ) ;
147+ Assert . True ( properties . TryGetProperty ( "name" , out _ ) , "Person should have a name property" ) ;
148+ }
149+
107150 [ Fact ]
108151 public void AsOpenAIResponseTool_ProducesValidInstance ( )
109152 {
0 commit comments