@@ -209,11 +209,13 @@ type PassthroughToolSchemaKey = "tools" | "tool_choice" | "parallel_tool_calls"
209209const normalizeCodexToolChoice = ( value : unknown ) : unknown => {
210210 if ( ! isRecord ( value ) ) return value ;
211211 if ( getString ( value . type ) !== "function" ) return value ;
212- if ( typeof value [ "name" ] === "string" && value [ "name" ] . trim ( ) ) return value ;
212+
213+ const topLevelName = getString ( value . name ) ;
213214 const fn = isRecord ( value . function ) ? value . function : null ;
214- if ( ! fn ) return value ;
215+ if ( ! fn && ! topLevelName ) return value ;
215216
216- const name = getString ( fn . name ) ;
217+ const functionName = fn ? getString ( fn . name ) : "" ;
218+ const name = topLevelName || functionName ;
217219 if ( ! name ) return value ;
218220
219221 const normalized : Record < string , unknown > = { ...value , name } ;
@@ -229,11 +231,15 @@ const normalizeCodexTools = (value: unknown): unknown => {
229231 const nestedFunction = isRecord ( tool . function ) ? tool . function : null ;
230232 if ( ! nestedFunction ) return tool ;
231233
232- const name = getString ( nestedFunction . name ) ;
233- if ( ! name ) return tool ;
234- if ( typeof tool . name === "string" && tool . name . trim ( ) ) return tool ;
234+ const nestedName = getString ( nestedFunction . name ) ;
235+ if ( ! nestedName ) return tool ;
236+
237+ const normalized : Record < string , unknown > = { ...tool } ;
238+
239+ // Prefer existing top-level name if present and non-empty; otherwise use nested function name.
240+ const existingName = getString ( normalized . name ) ;
241+ if ( ! existingName ) normalized . name = nestedName ;
235242
236- const normalized : Record < string , unknown > = { ...tool , name } ;
237243 if ( ! ( "description" in normalized ) && nestedFunction . description !== undefined ) {
238244 normalized . description = nestedFunction . description ;
239245 }
0 commit comments