adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

I want to get "real" stream response from Agent Engine

Open comlhj1114 opened this issue 1 year ago • 6 comments

I’ve deployed my agent to Vertex AI’s Agent Engine using Google’s ADK. However, even when I use streamQuery API, I’m not getting true streaming responses, which may include "partial: true". Instead, I receive responses in chunks at the object level — like complete text, function_call, or function_response objects — rather than a chunked text stream.

I’d like to get incremental streaming responses from the Agent Engine, not just whole-complete object-level streaming. Is this currently supported, or am I missing something in the setup?

comlhj1114 avatar Jun 19 '25 01:06 comlhj1114

I think, in vertexai's template, async_run is implemented in "event stream" not "text stream" like in adk due to the missing RunConfig.

// vertex ai (link)

        events_async = self._tmpl_attrs.get("runner").run_async(
            user_id=user_id, session_id=session_id, new_message=content, **kwargs
        )


        async for event in events_async:
            # Yield the event data as a dictionary
            yield event.model_dump(exclude_none=True)

// adk (link)

    async def event_generator():
      try:
        stream_mode = StreamingMode.SSE if req.streaming else StreamingMode.NONE
        runner = await _get_runner_async(req.app_name)
        async for event in runner.run_async(
            user_id=req.user_id,
            session_id=req.session_id,
            new_message=req.new_message,
            run_config=RunConfig(streaming_mode=stream_mode),
        ):
          # Format as SSE data
          sse_event = event.model_dump_json(exclude_none=True, by_alias=True)
          logger.info("Generated event in agent run streaming: %s", sse_event)
          yield f"data: {sse_event}\n\n"
      except Exception as e:
        logger.exception("Error in event_generator: %s", e)
        # You might want to yield an error event here
        yield f'data: {{"error": "{str(e)}"}}\n\n' 

comlhj1114 avatar Jun 23 '25 22:06 comlhj1114

It might be related to https://github.com/google/adk-python/issues/1099

comlhj1114 avatar Jun 24 '25 22:06 comlhj1114

This is a really necessary feature. When you want to create a response type=array for agent_tool, the model continuously accumulates streaming data until a long array is completed, but it does not return function_response as partial until the event is complete. I had a great experience with pydanticai's partial json parsing with a similar function, so I would really like to see it added.

jinookkoo avatar Jul 03 '25 06:07 jinookkoo

This workaround works for me

https://github.com/googleapis/python-aiplatform/issues/5351

comlhj1114 avatar Jul 09 '25 06:07 comlhj1114

+1, any update on this?

jheym avatar Oct 15 '25 00:10 jheym

It's supported now: https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/bidirectional-streaming

hangfei avatar Oct 24 '25 03:10 hangfei