Show the prompt length error when the AI Gateway returns 413
What does this MR do and why?
When the AI Gateway answers /v2/chat/agent with a 413, Duo Chat now shows the A1005 answer ("I'm sorry, you've entered too many prompts. Please run /clear or /reset before asking the next question.") instead of the generic A9999 answer.
The AI Gateway is adding a 413 limit for large request bodies, behind a FF (https://gitlab.com/gitlab-org/modelops/applied-ml/code-suggestions/ai-assist/-/work_items/3404). Today, Rails turns a 413 into Gitlab::AiGateway::ClientError, which falls through to A9999 ("Please try again."). A retry sends the same conversation, so it fails again.
In 7 days of production data, every chat request large enough to hit that limit already failed at the model, and the ones I checked failed with "prompt is too long", which Rails shows as A1005. So with this MR, the user gets the same answer when the AI Gateway rejects the request before it reaches the model.
I added Gitlab::AiGateway::RequestTooLargeError as a subclass of ClientError, so code that rescues ClientError keeps working.
References
- https://gitlab.com/gitlab-org/modelops/applied-ml/code-suggestions/ai-assist/-/work_items/3404
- Findings on how Rails handles a 413: https://gitlab.com/gitlab-org/modelops/applied-ml/code-suggestions/ai-assist/-/work_items/3404#note_4177477910
Screenshots or screen recordings
| Before | After |
|---|---|
I'm sorry, I can't generate a response. Please try again. (A9999) |
I'm sorry, you've entered too many prompts. Please run /clear or /reset before asking the next question. (A1005) |
How to set up and validate locally
-
In your local AI Gateway, make
/v2/chat/agentreturn a 413. For eg, add this as the first line ofchat()inai_gateway/api/v2/chat/agent.py:raise HTTPException(status_code=413, detail="Request body exceeds 16777216 bytes") -
Open Duo Chat in GDK and ask any question.
-
Before this MR, chat answers with error A9999. With this MR, chat answers with error A1005.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.