diff --git a/letta/agent.py b/letta/agent.py index 997287f1e6..6b6d4eb223 100644 --- a/letta/agent.py +++ b/letta/agent.py @@ -555,11 +555,11 @@ def _get_ai_reply( raise ValueError(f"API call returned an empty message: {response}") if response.choices[0].finish_reason not in ["stop", "function_call", "tool_calls"]: - raise ValueError(f"Bad finish reason from API: {response.choices[0].finish_reason}") - - # This is not retryable, hence RuntimeError v.s. ValueError - if response.choices[0].finish_reason == "length": - raise RuntimeError("Finish reason was length (maximum context length)") + if response.choices[0].finish_reason == "length": + # This is not retryable, hence RuntimeError v.s. ValueError + raise RuntimeError("Finish reason was length (maximum context length)") + else: + raise ValueError(f"Bad finish reason from API: {response.choices[0].finish_reason}") return response