From d1865eaa22bb9b46e3fdeeb05a61216ad1d93286 Mon Sep 17 00:00:00 2001 From: Andy Li <55300002+cliandy@users.noreply.github.com> Date: Fri, 2 May 2025 11:45:41 -0700 Subject: [PATCH] fix: handle malformed function calls in vertex (#1987) --- letta/llm_api/google_ai_client.py | 4 ++-- letta/llm_api/google_vertex_client.py | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/letta/llm_api/google_ai_client.py b/letta/llm_api/google_ai_client.py index 5f807f73..6f782c10 100644 --- a/letta/llm_api/google_ai_client.py +++ b/letta/llm_api/google_ai_client.py @@ -122,8 +122,8 @@ class GoogleAIClient(LLMClientBase): for candidate in response_data["candidates"]: content = candidate["content"] - if "role" not in content: - # This means the response is malformed + if "role" not in content or not content["role"]: + # This means the response is malformed like MALFORMED_FUNCTION_CALL # NOTE: must be a ValueError to trigger a retry raise ValueError(f"Error in response data from LLM: {response_data}") role = content["role"] diff --git a/letta/llm_api/google_vertex_client.py b/letta/llm_api/google_vertex_client.py index 177eac8d..15e610d4 100644 --- a/letta/llm_api/google_vertex_client.py +++ b/letta/llm_api/google_vertex_client.py @@ -110,7 +110,11 @@ class GoogleVertexClient(GoogleAIClient): for candidate in response.candidates: content = candidate.content - role = content.role + if "role" not in content or not content["role"]: + # This means the response is malformed like MALFORMED_FUNCTION_CALL + # NOTE: must be a ValueError to trigger a retry + raise ValueError(f"Error in response data from LLM: {response_data}") + role = content["role"] assert role == "model", f"Unknown role in response: {role}" parts = content.parts