@@ -1614,20 +1614,9 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
16141614 // name or arguments value fails closed through the #1325 channel here rather
16151615 // than escaping later as a TypeError from string handling at flush time.
16161616 const rawFunction = ( rawToolCall as { function ?: unknown } ) . function ;
1617- if ( rawFunction !== undefined && rawFunction !== null ) {
1618- if ( ! isRecord ( rawFunction ) ) {
1619- logInvalidToolCalls ( "stream" , rawToolCalls ) ;
1620- return yield * terminateWithError ( invalidToolCallsEvent ( rawToolCalls , "stream" , pendingUsage ) ) ;
1621- }
1622- const rawName = rawFunction . name ;
1623- const rawArguments = rawFunction . arguments ;
1624- // Some OpenAI-compatible streamers repeat already-sent fields as null on
1625- // continuation deltas. Treat only null/undefined as absent; every other
1626- // non-string value still fails closed before entering the accumulator.
1627- if ( isInvalidStreamStringField ( rawName ) || isInvalidStreamStringField ( rawArguments ) ) {
1628- logInvalidToolCalls ( "stream" , rawToolCalls ) ;
1629- return yield * terminateWithError ( invalidToolCallsEvent ( rawToolCalls , "stream" , pendingUsage ) ) ;
1630- }
1617+ if ( rawFunction !== undefined && rawFunction !== null && ! isRecord ( rawFunction ) ) {
1618+ logInvalidToolCalls ( "stream" , rawToolCalls ) ;
1619+ return yield * terminateWithError ( invalidToolCallsEvent ( rawToolCalls , "stream" , pendingUsage ) ) ;
16311620 }
16321621 if ( isInvalidStreamStringField ( tc . id ) ) {
16331622 logInvalidToolCalls ( "stream" , rawToolCalls ) ;
@@ -1646,14 +1635,33 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
16461635 budget . openCall ( call . key ) ;
16471636 }
16481637 if ( tc . id && ! call . id ) call . id = tc . id ;
1638+ // #1731 generalization: some gateways repeat the function envelope on
1639+ // continuation deltas with wrong JSON types (an object `name` observed from
1640+ // opencode.ai/zen) instead of null. Once the pending call carries its
1641+ // canonical name, a non-string repeat adds nothing — ignore it, and let only
1642+ // string fragments reach the accumulator. A non-string value while the call is
1643+ // still unnamed fails closed (#1531): it was the field's only chance to be
1644+ // established, and nothing else can name or parameterize the call.
1645+ const fnRecord = isRecord ( rawFunction ) ? rawFunction : undefined ;
1646+ if ( isInvalidStreamStringField ( fnRecord ?. name ) && ! call . name ) {
1647+ logInvalidToolCalls ( "stream" , rawToolCalls ) ;
1648+ return yield * terminateWithError ( invalidToolCallsEvent ( rawToolCalls , "stream" , pendingUsage ) ) ;
1649+ }
1650+ if ( isInvalidStreamStringField ( fnRecord ?. arguments ) && ! call . name ) {
1651+ logInvalidToolCalls ( "stream" , rawToolCalls ) ;
1652+ return yield * terminateWithError ( invalidToolCallsEvent ( rawToolCalls , "stream" , pendingUsage ) ) ;
1653+ }
16491654 if ( tc . function ?. name && ! call . name ) call . name = tc . function . name ;
1650- if ( tc . function ?. arguments ) {
1655+ // Only string fragments enter the accumulator: an accepted non-string repeat
1656+ // (padding, guarded above) must not concatenate "[object Object]" into args.
1657+ const argsDelta = typeof tc . function ?. arguments === "string" ? tc . function . arguments : "" ;
1658+ if ( argsDelta ) {
16511659 const previousBytes = call . argsBytes ;
1652- const nextBytes = previousBytes + budgetEncoder . encode ( tc . function . arguments ) . byteLength ;
1660+ const nextBytes = previousBytes + budgetEncoder . encode ( argsDelta ) . byteLength ;
16531661 const scope = { kind : "tool_args" as const , callId : call . key } ;
16541662 const reservation = budget . reserveTransient ( nextBytes , scope ) ;
16551663 try {
1656- call . args += tc . function . arguments ;
1664+ call . args += argsDelta ;
16571665 reservation . commitRetained ( ) ;
16581666 budget . releaseRetained ( previousBytes , scope ) ;
16591667 call . argsBytes = nextBytes ;
0 commit comments