From e633559244ad38f507a3e4ffca5e5d08a9e9e38f Mon Sep 17 00:00:00 2001 From: Savil Srivastava <676452+savil@users.noreply.github.com> Date: Wed, 21 Jan 2026 14:28:54 -0800 Subject: [PATCH] [aisdk] add Truncation: {auto, default} as an encoding metadata option --- .../provider/openai/internal/codec/encode.go | 24 +++++++++++++++---- .../openai/internal/codec/metadata.go | 10 ++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/aisdk/ai/provider/openai/internal/codec/encode.go b/aisdk/ai/provider/openai/internal/codec/encode.go index 8dbd8f9e..323884e8 100644 --- a/aisdk/ai/provider/openai/internal/codec/encode.go +++ b/aisdk/ai/provider/openai/internal/codec/encode.go @@ -118,10 +118,8 @@ func applyCallOptions(params *responses.ResponseNewParams, opts api.CallOptions, // Apply provider options from metadata applyProviderMetadata(params, opts) - // Apply model-specific settings - if modelConfig.RequiredAutoTruncation { - params.Truncation = "auto" - } + // Apply truncation settings: caller-provided takes precedence over model defaults + applyTruncationSettings(params, opts, modelConfig) // Apply reasoning settings and handle unsupported options for reasoning models reasoningWarnings := applyReasoningSettings(params, opts, modelConfig) @@ -185,6 +183,24 @@ func applyProviderMetadata(params *responses.ResponseNewParams, opts api.CallOpt } } +// applyTruncationSettings applies truncation settings to the parameters. +// Caller-provided truncation via metadata takes precedence over model defaults. +func applyTruncationSettings(params *responses.ResponseNewParams, opts api.CallOptions, modelConfig modelConfig) { + // Check if caller provided a truncation setting via metadata + if opts.ProviderMetadata != nil { + metadata := GetMetadata(&opts) + if metadata != nil && metadata.Truncation != "" { + params.Truncation = responses.ResponseNewParamsTruncation(metadata.Truncation) + return + } + } + + // Fall back to model-specific defaults + if modelConfig.RequiredAutoTruncation { + params.Truncation = "auto" + } +} + // applyReasoningSettings applies settings specific to reasoning models // and handles unsupported options func applyReasoningSettings(params *responses.ResponseNewParams, opts api.CallOptions, diff --git a/aisdk/ai/provider/openai/internal/codec/metadata.go b/aisdk/ai/provider/openai/internal/codec/metadata.go index c516ba2a..a9a1aef3 100644 --- a/aisdk/ai/provider/openai/internal/codec/metadata.go +++ b/aisdk/ai/provider/openai/internal/codec/metadata.go @@ -66,6 +66,16 @@ type Metadata struct { // Supported values are `concise` and `detailed`. ReasoningSummary string `json:"reasoning_summary,omitempty"` + // Truncation specifies the truncation strategy to use for the model input. + // When set, this overrides any model-specific default truncation behavior. + // + // Supported values are `auto` and `disabled` (default). + // - `auto`: If the context of this response and previous ones exceeds the + // model's context window size, the model will truncate the response to fit. + // - `disabled`: If the context exceeds the model's context window size, + // the request will fail with a 400 error. + Truncation string `json:"truncation,omitempty"` + // --- Used in blocks --- // ImageDetail indicates the level of detail that should be used when processing