Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions aisdk/ai/provider/openai/internal/codec/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions aisdk/ai/provider/openai/internal/codec/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading