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

Live Api with tools agents end with `extra_headers` issue

Open pretbc opened this issue 1 year ago • 5 comments

** Please make sure you read the contribution guide and file the issues in the right place. ** Contribution guide.

Describe the bug Hello there,

I want to create root agent who will have agent as a tool (2x) to pass them information gathered from AUDIO


affect_alignment_agent = Agent(
model="gemini-2.0-flash-exp",
name="affect_alignment_agent",
description="Affect alignment agent",
instruction=affect_aligment,
)


engagement_agent = Agent(
model="gemini-2.0-flash-exp",
name="engagement_agent",
description="Engagement agent",
instruction=engagement,
)


affect_alignment_tool = agent_tool.AgentTool(agent=affect_alignment_agent) # Wrap the agent
engagement_tool = agent_tool.AgentTool(agent=engagement_agent) # Wrap the agent


root_agent = Agent(
model="gemini-2.0-flash-exp",
name="root_agent",
description="Root agent that coordinates the affect alignment and engagement agents",
instruction="""
You are responsible for coordinating the affect alignment and engagement agents tools.
Send user input to the tools and wait for their responses.
Combine the responses from the tools and send the final response to the user.
""",
tools=[affect_alignment_tool, engagement_tool],
)



what I can see from the Raw response:


{"candidates":[{"content":{"parts":[{"text":"```text\nDetected Emotional State: Neutral (valence = 0.5, arousal = 0.5). Delta from baseline: No significant change.\nStrategy: Maintain a neutral and friendly tone as the user's emotional state is currently stable.\n\nResponse: \"Hello! I'm doing well, thank you for asking. How can I assist you today?\"\n```\n"}],"role":"model"},"finish_reason":"STOP","avg_logprobs":-0.13121765087812376}],"model_version":"gemini-2.0-flash-exp","usage_metadata":{"candidates_token_count":78,"candidates_tokens_details":[{"modality":"TEXT","token_count":78}],"prompt_token_count":1111,"prompt_tokens_details":[{"modality":"TEXT","token_count":1111}],"total_token_count":1189},"automatic_function_calling_history":[]}

I got some first response but later error occurred:


You are an agent. Your internal name is "affect_alignment_agent".

 The description about you is "Affect alignment agent"
-----------------------------------------------------------
Contents:
{"parts":[{"text":"Hello, how are you?"}],"role":"user"}
-----------------------------------------------------------
Functions:

-----------------------------------------------------------

2025-06-06 14:47:49,211 - INFO - models.py:7466 - AFC is enabled with max remote calls: 10.
2025-06-06 14:47:50,227 - INFO - _client.py:1740 - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent "HTTP/1.1 200 OK"
2025-06-06 14:47:50,230 - INFO - google_llm.py:167 - 
LLM Response:
-----------------------------------------------------------
Text:

Detected Emotional State: Neutral (valence = 0.5, arousal = 0.5). Delta from baseline: No significant change.
Strategy: Maintain a neutral and friendly tone as the user's emotional state is currently stable.

Response: "Hello! I'm doing well, thank you for asking. How can I assist you today?"


-----------------------------------------------------------
Function calls:

-----------------------------------------------------------
Raw response:
{"candidates":[{"content":{"parts":[{"text":"```text\nDetected Emotional State: Neutral (valence = 0.5, arousal = 0.5). Delta from baseline: No significant change.\nStrategy: Maintain a neutral and friendly tone as the user's emotional state is currently stable.\n\nResponse: \"Hello! I'm doing well, thank you for asking. How can I assist you today?\"\n```\n"}],"role":"model"},"finish_reason":"STOP","avg_logprobs":-0.13121765087812376}],"model_version":"gemini-2.0-flash-exp","usage_metadata":{"candidates_token_count":78,"candidates_tokens_details":[{"modality":"TEXT","token_count":78}],"prompt_token_count":1111,"prompt_tokens_details":[{"modality":"TEXT","token_count":1111}],"total_token_count":1189},"automatic_function_calling_history":[]}
-----------------------------------------------------------

2025-06-06 14:47:50,279 - ERROR - fast_api.py:908 - Error during live websocket communication: BaseEventLoop.create_connection() got an unexpected keyword argument 'extra_headers'
Traceback (most recent call last):
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/genai/live.py", line 1046, in connect
    yield AsyncSession(api_client=self._api_client, websocket=ws)
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/models/google_llm.py", line 243, in connect
    yield GeminiLlmConnection(live_session)
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 115, in run_live
    async for event in self._receive_from_model(
    ...<36 lines>...
        return
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 246, in _receive_from_model
    async for event in self._postprocess_live(
    ...<23 lines>...
      yield event
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 439, in _postprocess_live
    function_response_event = await functions.handle_function_calls_live(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        invocation_context, model_response_event, llm_request.tools_dict
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/functions.py", line 288, in handle_function_calls_live
    trace_tool_call(
    ~~~~~~~~~~~~~~~^
        tool=tool,
        ^^^^^^^^^^
    ...<2 lines>...
        function_response=function_response,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
TypeError: trace_tool_call() got an unexpected keyword argument 'response_event_id'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/cli/fast_api.py", line 904, in agent_live_run
    task.result()
    ~~~~~~~~~~~^^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/cli/fast_api.py", line 877, in forward_events
    async for event in runner.run_live(
    ...<4 lines>...
      )
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 325, in run_live
    async for event in invocation_context.agent.run_live(invocation_context):
      await self.session_service.append_event(session=session, event=event)
      yield event
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/agents/base_agent.py", line 174, in run_live
    async for event in self._run_live_impl(ctx):
      yield event
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/agents/llm_agent.py", line 286, in _run_live_impl
    async for event in self._llm_flow.run_live(ctx):
      self.__maybe_save_output_to_state(event)
      yield event
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 87, in run_live
    async with llm.connect(llm_request) as llm_connection:
               ~~~~~~~~~~~^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.3/Frameworks/Python.framework/Versions/3.13/lib/python3.13/contextlib.py", line 235, in __aexit__
    await self.gen.athrow(value)
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/models/google_llm.py", line 240, in connect
    async with self._live_api_client.aio.live.connect(
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        model=llm_request.model, config=llm_request.live_connect_config
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ) as live_session:
    ^
  File "/opt/homebrew/Cellar/[email protected]/3.13.3/Frameworks/Python.framework/Versions/3.13/lib/python3.13/contextlib.py", line 235, in __aexit__
    await self.gen.athrow(value)
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/genai/live.py", line 1049, in connect
    async with ws_connect(uri, extra_headers=headers) as ws:
               ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/websockets/asyncio/client.py", line 587, in __aenter__
    return await self
           ^^^^^^^^^^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/websockets/asyncio/client.py", line 541, in __await_impl__
    self.connection = await self.create_connection()
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/websockets/asyncio/client.py", line 467, in create_connection
    _, connection = await loop.create_connection(factory, **kwargs)
                          ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
TypeError: BaseEventLoop.create_connection() got an unexpected keyword argument 'extra_headers'
Traceback (most recent call last):
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/genai/live.py", line 1046, in connect
    yield AsyncSession(api_client=self._api_client, websocket=ws)
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/models/google_llm.py", line 243, in connect
    yield GeminiLlmConnection(live_session)
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 115, in run_live
    async for event in self._receive_from_model(
    ...<36 lines>...
        return
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 246, in _receive_from_model
    async for event in self._postprocess_live(
    ...<23 lines>...
      yield event
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 439, in _postprocess_live
    function_response_event = await functions.handle_function_calls_live(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        invocation_context, model_response_event, llm_request.tools_dict
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/functions.py", line 288, in handle_function_calls_live
    trace_tool_call(
    ~~~~~~~~~~~~~~~^
        tool=tool,
        ^^^^^^^^^^
    ...<2 lines>...
        function_response=function_response,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
TypeError: trace_tool_call() got an unexpected keyword argument 'response_event_id'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/cli/fast_api.py", line 904, in agent_live_run
    task.result()
    ~~~~~~~~~~~^^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/cli/fast_api.py", line 877, in forward_events
    async for event in runner.run_live(
    ...<4 lines>...
      )
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 325, in run_live
    async for event in invocation_context.agent.run_live(invocation_context):
      await self.session_service.append_event(session=session, event=event)
      yield event
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/agents/base_agent.py", line 174, in run_live
    async for event in self._run_live_impl(ctx):
      yield event
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/agents/llm_agent.py", line 286, in _run_live_impl
    async for event in self._llm_flow.run_live(ctx):
      self.__maybe_save_output_to_state(event)
      yield event
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 87, in run_live
    async with llm.connect(llm_request) as llm_connection:
               ~~~~~~~~~~~^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.3/Frameworks/Python.framework/Versions/3.13/lib/python3.13/contextlib.py", line 235, in __aexit__
    await self.gen.athrow(value)
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/adk/models/google_llm.py", line 240, in connect
    async with self._live_api_client.aio.live.connect(
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        model=llm_request.model, config=llm_request.live_connect_config
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ) as live_session:
    ^
  File "/opt/homebrew/Cellar/[email protected]/3.13.3/Frameworks/Python.framework/Versions/3.13/lib/python3.13/contextlib.py", line 235, in __aexit__
    await self.gen.athrow(value)
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/google/genai/live.py", line 1049, in connect
    async with ws_connect(uri, extra_headers=headers) as ws:
               ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/websockets/asyncio/client.py", line 587, in __aenter__
    return await self
           ^^^^^^^^^^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/websockets/asyncio/client.py", line 541, in __await_impl__
    self.connection = await self.create_connection()
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pretbc/Workspace/affect-gadk-prompts/.venv/lib/python3.13/site-packages/websockets/asyncio/client.py", line 467, in create_connection
    _, connection = await loop.create_connection(factory, **kwargs)
                          ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
TypeError: BaseEventLoop.create_connection() got an unexpected keyword argument 'extra_headers'

Also Im not pretty sure that these response was base on AUDIO. It looks like it is base on TEXT only. Is this possible to create custom runner with artifact_service=artifact_service and base on that other agents will be able to access AUDIO ?

To Reproduce Steps to reproduce the behavior:

  1. Create agents from my code
  2. Run using adk web and enable MIC
  3. See error logs

Expected behavior I would expect that agent will run tools and return to the user

Desktop (please complete the following information):

  • OS: MacBook
  • Python version(python -V): 3.13
  • ADK version(pip show google-adk): 1.2.1

pretbc avatar Jun 06 '25 13:06 pretbc

@pretbc I have the same issue. After some trial and error it works for me if I change

.venv/Lib/site-packages/google/adk/flows/llm_flows/functions.py line 288 to

      trace_tool_call(
          tool=tool,
          args=function_args,
          #response_event_id=function_response_event.id,
          function_response_event=function_response_event,
      )

if you look at line 194 in the same file there they do invoke the method with the code below so I think its just an oversight

      trace_tool_call(
          tool=tool,
          args=function_args,
          function_response_event=function_response_event,
      )

It seems like otherwise a typeError is thrown because the trace_tool_call expects other inputs than the ones provided in the code. This typeError is then try/excepted in .venv/Lib/site-packages/google/genai/live.py line 1041. When this error happens the code tries to use an older version of websockets that uses the "extra_headers" argument instead of the "additional_headers" argument and that clashes with the websocket version requirements for the google-adk package

Ps: this issue only occurs for me when the agent is calling tools. Just telling "hi gemini" at the start will not reproduce the error

PPs: I see there is an open PR already to fix this PR

Rik-devoteam avatar Jun 07 '25 12:06 Rik-devoteam

@Rik-devoteam Thanks , the above fix solved the issue. Lets wait for the above PR to merged and for a new build after that. Will live with locally edited file until that.

jobmthomas avatar Jun 07 '25 20:06 jobmthomas

seems like your suggestion @Rik-devoteam work well !!!

pretbc avatar Jun 09 '25 05:06 pretbc

@Rik-devoteam thank you for your suggestion. It solved my error.

torbilote avatar Jun 12 '25 12:06 torbilote

Hi @pretbc and thanks to everyone who participated in this thread.

We're currently doing some housekeeping on our issue tracker. Given the rapid pace of development and the number of updates since this was last active, there's a good chance this issue has been resolved in a newer version. To help us keep our backlog focused on current issues, we are closing this as stale.

If you're still facing this problem, could you please update to the very latest version of the library and confirm the issue still exists? If it does, please comment here, and we will be happy to reopen this issue and investigate further.

Thanks for your contribution and understanding!

jwiejulie avatar Aug 14 '25 20:08 jwiejulie