linshenkx/prompt-optimizer · error · OptimizationError
Target prompt is required
Error message
Target prompt is required
What it means
Optimization requests must include a non-empty targetPrompt; validateOptimizationRequest throws OptimizationError when targetPrompt is missing, empty, or only whitespace. This guard runs before any model or template work in both optimizePrompt and optimizePromptStream.
Source
Thrown at packages/core/src/services/prompt/service.ts:849
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
throw new IterationError(
originalPrompt,
iterateInput,
`Iteration failed: ${errorMessage}`,
);
}
}
// === 新增:支持提示词类型的增强方法 ===
/**
* 验证优化请求参数
*/
private validateOptimizationRequest(request: OptimizationRequest) {
if (!request.targetPrompt?.trim()) {
throw new OptimizationError("", "Target prompt is required");
}
if (!request.modelKey?.trim()) {
throw new OptimizationError(request.targetPrompt, "Model key is required");
}
}
private hasInputImages(request: OptimizationRequest): request is OptimizationRequest & { inputImages: NonNullable<OptimizationRequest["inputImages"]> } {
return Array.isArray(request.inputImages) && request.inputImages.length > 0;
}
private hasTestInputImages(inputImages?: ImageInputRef[]): inputImages is ImageInputRef[] {
return Array.isArray(inputImages) && inputImages.length > 0;
}
private getSafeTestErrorMessage(error: unknown, inputImages?: ImageInputRef[]): string {
let message = error instanceof Error ? error.message : String(error);
if (!this.hasTestInputImages(inputImages)) {
return message;View on GitHub (pinned to 3e677b1d9f)
Solutions
- Validate and trim targetPrompt before calling the API
- Disable the optimize action in the UI until a non-empty prompt is entered
- Log the request payload shape in tests to catch missing fields early
Example fix
// before
await service.optimizePrompt({ targetPrompt: ' ', modelKey });
// after
const target = targetPrompt?.trim();
if (!target) throw new Error('Enter a prompt to optimize');
await service.optimizePrompt({ targetPrompt: target, modelKey }); Defensive patterns
Strategy: validation
Validate before calling
const target = request.targetPrompt?.trim() ?? '';
if (!target) throw new Error('targetPrompt is required'); Type guard
const hasTargetPrompt = ( r: Partial<OptimizationRequest> ): r is OptimizationRequest => typeof r.targetPrompt === 'string' && r.targetPrompt.trim().length > 0;
Try / catch
try {
await svc.optimizePrompt(req);
} catch (e) {
if (e instanceof OptimizationError && e.message === 'Target prompt is required') {
// show empty-input validation message to user
} else throw e;
} Prevention
- Trim and check required string fields before calling optimize APIs
- Disable submit actions until required inputs are non-empty
- Write unit tests covering empty/whitespace request fields
When it happens
Trigger: Calling optimizePrompt/optimizePromptStream with targetPrompt undefined, '', or a string of spaces/newlines; e.g. submitting the optimize form before typing a prompt.
Common situations: UI form submitted before user input; programmatic callers passing an object built from empty state; trailing-whitespace-only strings from copy/paste.
Understand the failure class
Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.
Related errors
- ${label} testCaseId must not be empty.
- Function mode (functionMode) cannot be empty
- CONFIG_INVALID
- Model config cannot be empty
- User prompt is required
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/3d674acdef418244.
Report an issue: GitHub.