Skip to content

Commit 840f639

Browse files
authored
Show users a special message for context length errors (#1337)
* Show users a special message for context length errors * More helpful error message * More helpful error message
1 parent ad6fa14 commit 840f639

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

app/backend/error.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
"""
1010
ERROR_MESSAGE_FILTER = """Your message contains content that was flagged by the OpenAI content filter."""
1111

12+
ERROR_MESSAGE_LENGTH = """Your message exceeded the context length limit for this OpenAI model. Please shorten your message or change your settings to retrieve fewer search results."""
13+
1214

1315
def error_dict(error: Exception) -> dict:
1416
if isinstance(error, APIError) and error.code == "content_filter":
1517
return {"error": ERROR_MESSAGE_FILTER}
18+
if isinstance(error, APIError) and error.code == "context_length_exceeded":
19+
return {"error": ERROR_MESSAGE_LENGTH}
1620
return {"error": ERROR_MESSAGE.format(error_type=type(error))}
1721

1822

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"error": "Your message exceeded the context length limit for this OpenAI model. Please shorten your message or change your settings to retrieve fewer search results."
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"error": "Your message exceeded the context length limit for this OpenAI model. Please shorten your message or change your settings to retrieve fewer search results."
3+
}

tests/test_app.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ def fake_response(http_code):
3030
),
3131
)
3232

33+
contextlength_response = BadRequestError(
34+
message="This model's maximum context length is 4096 tokens. However, your messages resulted in 5069 tokens. Please reduce the length of the messages.",
35+
body={
36+
"message": "This model's maximum context length is 4096 tokens. However, your messages resulted in 5069 tokens. Please reduce the length of the messages.",
37+
"code": "context_length_exceeded",
38+
"status": 400,
39+
},
40+
response=Response(400, request=Request(method="get", url="https://foo.bar/"), json={"error": {"code": "429"}}),
41+
)
42+
3343

3444
def thoughts_contains_text(thoughts, text):
3545
found = False
@@ -115,6 +125,26 @@ async def test_ask_handle_exception_contentsafety(client, monkeypatch, snapshot,
115125
snapshot.assert_match(json.dumps(result, indent=4), "result.json")
116126

117127

128+
@pytest.mark.asyncio
129+
async def test_ask_handle_exception_contextlength(client, monkeypatch, snapshot, caplog):
130+
monkeypatch.setattr(
131+
"approaches.retrievethenread.RetrieveThenReadApproach.run",
132+
mock.Mock(side_effect=contextlength_response),
133+
)
134+
135+
response = await client.post(
136+
"/ask",
137+
json={"messages": [{"content": "Super long message with lots of sources.", "role": "user"}]},
138+
)
139+
assert response.status_code == 500
140+
result = await response.get_json()
141+
assert (
142+
"Exception in /ask: This model's maximum context length is 4096 tokens. However, your messages resulted in 5069 tokens. Please reduce the length of the messages."
143+
in caplog.text
144+
)
145+
snapshot.assert_match(json.dumps(result, indent=4), "result.json")
146+
147+
118148
@pytest.mark.asyncio
119149
async def test_ask_rtr_text(client, snapshot):
120150
response = await client.post(

0 commit comments

Comments
 (0)