From 7a6c1f9386676e186a268250e53e16a7c190bca9 Mon Sep 17 00:00:00 2001 From: Jason Xu Date: Thu, 15 Oct 2026 07:52:13 +0800 Subject: [PATCH] fix: floor responses max_output_tokens to 16 for tiny budgets (#63) Zen's Responses endpoint rejects max_output_tokens below 16, and clients such as Claude Code send tiny budgets for background tasks. The 400 was retried across every anonymous exit before reaching the client. Raise budgets in (0, 16) to 16 on both the bridged and the same-protocol Responses paths; nil and larger budgets pass through unchanged. Co-authored-by: ahoo --- internal/protocol/request.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/protocol/request.go b/internal/protocol/request.go index 580d085..4ba8cf2 100644 --- a/internal/protocol/request.go +++ b/internal/protocol/request.go @@ -19,6 +19,11 @@ func PrepareRequest(from, to Protocol, input map[string]any, upstreamURL string) if err != nil { return nil, err } + if to == Responses { + // The bridge already floors converted bodies; a same-protocol body is + // a clone of the client's and needs the same guard. + jsonutil.Put(output, "max_output_tokens", floorResponsesMaxTokens(output["max_output_tokens"])) + } normalizeToolReasoningHistory(to, jsonutil.StringAt(output, "model"), upstreamURL, output) return output, nil } @@ -916,11 +921,26 @@ func responsesReasoning(value any) any { } } +// minResponsesMaxOutputTokens is the smallest max_output_tokens the Zen +// Responses endpoint accepts; anything lower is rejected with a 400. +const minResponsesMaxOutputTokens = 16 + +// floorResponsesMaxTokens keeps tiny downstream budgets (background titles, +// probes) from failing Zen responses validation. Larger budgets pass through +// untouched. Decoded JSON numbers are always float64, so no other numeric type +// can reach this point. +func floorResponsesMaxTokens(value any) any { + if n, ok := value.(float64); ok && n > 0 && n < minResponsesMaxOutputTokens { + return float64(minResponsesMaxOutputTokens) + } + return value +} + func encodeResponsesRequest(request bridgeRequest) map[string]any { output := map[string]any{"model": request.Model, "stream": request.Stream} jsonutil.Put(output, "temperature", request.Temperature) jsonutil.Put(output, "top_p", request.TopP) - jsonutil.Put(output, "max_output_tokens", request.MaxTokens) + jsonutil.Put(output, "max_output_tokens", floorResponsesMaxTokens(request.MaxTokens)) jsonutil.Put(output, "stop", request.Stop) jsonutil.Put(output, "metadata", request.Metadata) if len(request.System) > 0 {