Skip to content

Fix: Return 400 for template validation errors instead of 500 - #18

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 2 commits into
v1.4.0from
fix/template-error-status-code
Aug 19, 2026
Merged

Fix: Return 400 for template validation errors instead of 500#18
gh-worker-dd-mergequeue-cf854d[bot] merged 2 commits into
v1.4.0from
fix/template-error-status-code

Conversation

@dengkliu92

@dengkliu92 dengkliu92 commented Aug 19, 2026

Copy link
Copy Markdown

Template rendering errors (e.g., 'Unexpected message role', malformed chat history) are client validation errors and should return HTTP 400 Bad Request, not 500 Internal Server Error.

Previously, these errors were propagated as generic anyhow::Error, which ErrorMessage::from_anyhow() mapped to 500. This fix explicitly converts template errors to DynamoError::InvalidArgument, which the HTTP layer correctly maps to 400.

Fixes incorrect status codes for errors like:

  • 'Unexpected message role' (in tool_use context)
  • Invalid chat message structure
  • Template parameter validation failures

Before: HTTP 500 'Internal server error: Failed to generate completions...'
After: HTTP 400 'Failed to apply prompt template: ...'

Template rendering errors (e.g., 'Unexpected message role', malformed
chat history) are client validation errors and should return HTTP 400
Bad Request, not 500 Internal Server Error.

Previously, these errors were propagated as generic anyhow::Error,
which ErrorMessage::from_anyhow() mapped to 500. This fix explicitly
converts template errors to DynamoError::InvalidArgument, which the
HTTP layer correctly maps to 400.

Fixes incorrect status codes for errors like:
- 'Unexpected message role' (in tool_use context)
- Invalid chat message structure
- Template parameter validation failures

Before: HTTP 500 'Internal server error: Failed to generate completions...'
After:  HTTP 400 'Failed to apply prompt template: ...'

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the LLM request preprocessing path so prompt-template rendering/validation failures are surfaced as DynamoError with ErrorType::InvalidArgument, allowing the OpenAI HTTP layer to correctly return HTTP 400 (Bad Request) instead of HTTP 500 (Internal Server Error) for client-caused template issues.

Changes:

  • Convert apply_template() failures into ErrorType::InvalidArgument at the preprocessing call site to drive 400 responses.
  • Update the returned error message to include template-application context for client-facing errors.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/llm/src/preprocessor.rs
let _nvtx = dynamo_nvtx_range!("preprocess.template");
self.apply_template(request)
.with_context(|| "Failed to apply prompt template")?
.map_err(|e| {
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: dengkliu92 <dengke.liu@datadoghq.com>
@dengkliu92
dengkliu92 deployed to external_collaborator August 19, 2026 21:00 — with GitHub Actions Active
@dengkliu92
dengkliu92 deployed to external_collaborator August 19, 2026 21:00 — with GitHub Actions Active
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit c26a919 into v1.4.0 Aug 19, 2026
12 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants