valuecell icon indicating copy to clipboard operation
valuecell copied to clipboard

NewsAgent.stream yields raw dict on error instead of StreamResponse, causing GenericAgentExecutor to raise ValueError`

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments


Affected File & Lines

  • python/valuecell/agents/news_agent/core.py:75-78
  • python/valuecell/core/agent/decorator.py:185-188

Severity & Impact

  • Severity: High
  • Impact: System reliability and error handling crash. When NewsAgent encounters an upstream failure (e.g. news provider timeout, bad payload, API rate limit), it attempts to return an error by yielding {"type": "error", "content": ...}. The A2A decorator (GenericAgentExecutor) expects only StreamResponse or NotifyResponse objects. Yielding a dictionary causes decorator.py to raise an uncaught ValueError: Agent NewsAgent yielded invalid response type: <class 'dict'>. This masks the underlying error from both server logs and the frontend client, leaving tasks in a failed state with cryptic messages.

Root Cause Analysis

In python/valuecell/agents/news_agent/core.py:75-78:

except Exception as e:
    logger.error(f"Error processing news query: {str(e)}")
    logger.exception("Full error details:")
    yield {"type": "error", "content": f"Error processing news query: {str(e)}"}

In python/valuecell/core/agent/decorator.py:185-188:

async for response in query_handler(query, context_id, task_id, dependencies):
    if not isinstance(response, (StreamResponse, NotifyResponse)):
        raise ValueError(
            f"Agent {agent_name} yielded invalid response type: {type(response)}"
        )

Every streaming agent in ValueCell must yield StreamResponse instances. The core response factory (valuecell/core/agent/responses.py) provides streaming.failed(content) for this exact purpose:

def failed(self, content: Optional[str] = None) -> StreamResponse:
    return StreamResponse(content=content, event=TaskStatusEvent.TASK_FAILED)

Steps to Reproduce

Run the following script:

import sys
sys.path.insert(0, r"python")

from valuecell.core.types import StreamResponse, NotifyResponse

class MockNewsAgent:
    def stream_with_error(self):
        try:
            raise ConnectionError("Upstream news API timeout / rate limit")
        except Exception as e:
            # Matches line 78 in python/valuecell/agents/news_agent/core.py
            yield {"type": "error", "content": f"Error processing news query: {str(e)}"}

agent = MockNewsAgent()
for response in agent.stream_with_error():
    if not isinstance(response, (StreamResponse, NotifyResponse)):
        raise ValueError(f"Agent NewsAgent yielded invalid response type: {type(response)}")

Observed Behavior (Evidence)

Traceback (most recent call last):
  File "test_bug3_newsagent.py", line 18, in <module>
    raise ValueError(f"Agent NewsAgent yielded invalid response type: {type(response)}")
ValueError: Agent NewsAgent yielded invalid response type: <class 'dict'>

Expected Behavior

NewsAgent.stream() should yield a valid StreamResponse utilizing streaming.failed(...), allowing GenericAgentExecutor to handle task status transitions cleanly without throwing a type validation exception.

Proposed Fix

--- a/python/valuecell/agents/news_agent/core.py
+++ b/python/valuecell/agents/news_agent/core.py
@@ -75,4 +75,4 @@ class NewsAgent:
         except Exception as e:
             logger.error(f"Error processing news query: {str(e)}")
             logger.exception("Full error details:")
-            yield {"type": "error", "content": f"Error processing news query: {str(e)}"}
+            yield streaming.failed(f"Error processing news query: {str(e)}")

codeCraft-Ritik avatar Oct 27 '26 05:10 codeCraft-Ritik