Skip to content

Commit c4cb9a0

Browse files
authored
[aisdk] add Truncation: {auto, default} as an encoding metadata option (#592)
## Summary In `getModelConfig`, we specify some default settings for the openai models. In particular, the RequiredAutoTruncation is hardcoded in there. Some users may want to override this and manually set the truncation to auto or disabled, depending on their specific use-case. This PR allows that. ## How was it tested? Did not do any rigorous testing aside from compiling, and code inspection. We can consider adding some test-cases here in the future. ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request I represent that I have the right to license the contributions to the project maintainers under the Apache 2 License as stated in the [Community Contribution License](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license).
1 parent f60b696 commit c4cb9a0

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

aisdk/ai/provider/openai/internal/codec/encode.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ func applyCallOptions(params *responses.ResponseNewParams, opts api.CallOptions,
118118
// Apply provider options from metadata
119119
applyProviderMetadata(params, opts)
120120

121-
// Apply model-specific settings
122-
if modelConfig.RequiredAutoTruncation {
123-
params.Truncation = "auto"
124-
}
121+
// Apply truncation settings: caller-provided takes precedence over model defaults
122+
applyTruncationSettings(params, opts, modelConfig)
125123

126124
// Apply reasoning settings and handle unsupported options for reasoning models
127125
reasoningWarnings := applyReasoningSettings(params, opts, modelConfig)
@@ -185,6 +183,24 @@ func applyProviderMetadata(params *responses.ResponseNewParams, opts api.CallOpt
185183
}
186184
}
187185

186+
// applyTruncationSettings applies truncation settings to the parameters.
187+
// Caller-provided truncation via metadata takes precedence over model defaults.
188+
func applyTruncationSettings(params *responses.ResponseNewParams, opts api.CallOptions, modelConfig modelConfig) {
189+
// Check if caller provided a truncation setting via metadata
190+
if opts.ProviderMetadata != nil {
191+
metadata := GetMetadata(&opts)
192+
if metadata != nil && metadata.Truncation != "" {
193+
params.Truncation = responses.ResponseNewParamsTruncation(metadata.Truncation)
194+
return
195+
}
196+
}
197+
198+
// Fall back to model-specific defaults
199+
if modelConfig.RequiredAutoTruncation {
200+
params.Truncation = "auto"
201+
}
202+
}
203+
188204
// applyReasoningSettings applies settings specific to reasoning models
189205
// and handles unsupported options
190206
func applyReasoningSettings(params *responses.ResponseNewParams, opts api.CallOptions,

aisdk/ai/provider/openai/internal/codec/metadata.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ type Metadata struct {
6666
// Supported values are `concise` and `detailed`.
6767
ReasoningSummary string `json:"reasoning_summary,omitempty"`
6868

69+
// Truncation specifies the truncation strategy to use for the model input.
70+
// When set, this overrides any model-specific default truncation behavior.
71+
//
72+
// Supported values are `auto` and `disabled` (default).
73+
// - `auto`: If the context of this response and previous ones exceeds the
74+
// model's context window size, the model will truncate the response to fit.
75+
// - `disabled`: If the context exceeds the model's context window size,
76+
// the request will fail with a 400 error.
77+
Truncation string `json:"truncation,omitempty"`
78+
6979
// --- Used in blocks ---
7080

7181
// ImageDetail indicates the level of detail that should be used when processing

0 commit comments

Comments
 (0)