Overview
Validates and filters LLM completion parameters against model-specific parameter rules, removing unsupported or out-of-range values.
Description
This utility module provides functions to merge and validate completion parameters (such as temperature, top_p, stop sequences, etc.) used when invoking LLM models. The core function mergeValidCompletionParams iterates over a set of existing parameters, checks each against a set of ModelParameterRule definitions, and produces a cleaned parameter set containing only valid entries. Parameters that fail validation are tracked in a removedDetails record with human-readable reasons (e.g., "unsupported", "invalid type", "out of range"). A companion async function fetchAndMergeValidCompletionParams fetches the parameter rules from the API before performing the merge, providing an end-to-end validation workflow.
Usage
Use mergeValidCompletionParams when you already have parameter rules available locally and need to sanitize user-provided completion parameters before sending them to an LLM provider. Use fetchAndMergeValidCompletionParams when you need to dynamically retrieve the parameter rules for a specific model provider and model ID from the backend API, then validate against them.
Code Reference
Source Location
Signature
export const mergeValidCompletionParams = (
oldParams: FormValue | undefined,
rules: ModelParameterRule[],
isAdvancedMode: boolean = false,
): { params: FormValue, removedDetails: Record<string, string> }
export const fetchAndMergeValidCompletionParams = async (
provider: string,
modelId: string,
oldParams: FormValue | undefined,
isAdvancedMode: boolean = false,
): Promise<{ params: FormValue, removedDetails: Record<string, string> }>
Import
import { mergeValidCompletionParams, fetchAndMergeValidCompletionParams } from '@/utils/completion-params'
I/O Contract
Inputs (mergeValidCompletionParams)
| Name |
Type |
Required |
Description
|
| oldParams |
undefined |
Yes |
The existing completion parameters to validate
|
| rules |
ModelParameterRule[] |
Yes |
Array of parameter rules defining valid names, types, and ranges
|
| isAdvancedMode |
boolean |
No (default: false) |
When true, preserves the "stop" parameter even if no matching rule exists
|
Inputs (fetchAndMergeValidCompletionParams)
| Name |
Type |
Required |
Description
|
| provider |
string |
Yes |
The model provider identifier (e.g., "openai")
|
| modelId |
string |
Yes |
The model identifier (e.g., "gpt-4")
|
| oldParams |
undefined |
Yes |
The existing completion parameters to validate
|
| isAdvancedMode |
boolean |
No (default: false) |
When true, preserves the "stop" parameter
|
Outputs
| Name |
Type |
Description
|
| params |
FormValue |
The validated parameters containing only entries that pass rule checks
|
| removedDetails |
Record<string, string> |
A map of removed parameter names to human-readable reasons for removal
|
Usage Examples
Validate Parameters Against Known Rules
import { mergeValidCompletionParams } from '@/utils/completion-params'
const oldParams = { temperature: 0.7, top_p: 1.5, unknown_param: 'abc' }
const rules = [
{ name: 'temperature', type: 'float', min: 0, max: 1 },
{ name: 'top_p', type: 'float', min: 0, max: 1 },
]
const { params, removedDetails } = mergeValidCompletionParams(oldParams, rules)
// params: { temperature: 0.7 }
// removedDetails: { top_p: 'out of range (0-1)', unknown_param: 'unsupported' }
Fetch Rules and Validate in One Step
import { fetchAndMergeValidCompletionParams } from '@/utils/completion-params'
const { params, removedDetails } = await fetchAndMergeValidCompletionParams(
'openai',
'gpt-4',
{ temperature: 0.8, max_tokens: 2048 },
)
// params contains only parameters valid for the gpt-4 model
Related Pages