Fix: Return 400 for template validation errors instead of 500 - #18
Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 2 commits intoAug 19, 2026
Merged
Conversation
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: ...'
There was a problem hiding this comment.
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 intoErrorType::InvalidArgumentat 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.
| 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>
gh-worker-dd-mergequeue-cf854d
Bot
merged commit Aug 19, 2026
c26a919
into
v1.4.0
12 of 52 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Before: HTTP 500 'Internal server error: Failed to generate completions...'
After: HTTP 400 'Failed to apply prompt template: ...'