Compare commits

...
Author SHA1 Message Date
google-labs-jules[bot] d84e7af4ba Fix: Gracefully handle connection errors in chat completion
This commit addresses an issue where a connection error during
streaming chat completion (e.g., from an Ollama backend via LiteLLM)
would lead to a `AttributeError: 'NoneType' object has no attribute 'choices'`.

The fix involves the following changes:

1.  Modified `_process_stream_response` in `agent.py` to re-raise
    exceptions encountered during stream processing, instead of returning `None`.
2.  Adjusted `_chat_completion` in `agent.py` to also re-raise
    exceptions caught from its operations (including those from
    `_process_stream_response`), ensuring errors propagate correctly.
3.  Added a comment in `_process_stream_response` to guide you
    on troubleshooting common causes of connection errors, such as
    Ollama server issues or network misconfigurations.

These changes prevent the specific `AttributeError` by ensuring that
error conditions are propagated as exceptions, allowing for more
robust error handling and clearer diagnostics of underlying problems.
2025-06-19 04:16:46 +00:00
@@ -958,8 +958,14 @@ Your Goal: {self.goal}
return final_response
except Exception as e:
display_error(f"Error in stream processing: {e}")
return None
# Optionally, log the error here if display_error also logged
# import logging
# logging.error(f"Error in stream processing: {e}", exc_info=True)
# Note: If 'e' is a ConnectionError or similar, it often indicates
# an issue with the LLM backend (e.g., Ollama server unavailable,
# network misconfiguration, or incorrect OPENAI_API_BASE if using LiteLLM).
# Ensure the target LLM server is running and accessible.
raise e
def _chat_completion(self, messages, temperature=0.2, tools=None, stream=True, reasoning_steps=False):
start_time = time.time()
@@ -1117,8 +1123,11 @@ Your Goal: {self.goal}
return final_response
except Exception as e:
display_error(f"Error in chat completion: {e}")
return None
# Optional: Log the error here if display_error also logged or if more context is needed.
# import logging
# logging.error(f"Error in chat completion for agent {self.name}: {e}", exc_info=True)
display_error(f"Error in chat completion: {e}") # Keep if console feedback is desired before raising
raise # Re-raise the caught exception
def chat(self, prompt, temperature=0.2, tools=None, output_json=None, output_pydantic=None, reasoning_steps=False, stream=True):
# Log all parameter values when in debug mode