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

Cannot use VertexAI Llama4 Maas API

Open XinyueZ opened this issue 1 year ago • 3 comments

Hi,

I followed your document to connect the MAAS API: https://google.github.io/adk-docs/agents/models/#model-garden-deployments.

import os

os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
os.environ["GOOGLE_CLOUD_PROJECT"] = "some project id"
os.environ["GOOGLE_CLOUD_LOCATION"] = "europe-west4"

AGENT_MODEL = f"projects/{os.getenv('GOOGLE_CLOUD_PROJECT')}/locations/{os.getenv('GOOGLE_CLOUD_LOCATION')}/endpoints/publishers/meta/models/llama-4-maverick-17b-128e-instruct-maas"
print(AGENT_MODEL)


from google.adk.agents import Agent

chat_agent = Agent(
    name="chat_agent_v1",
    model=AGENT_MODEL,
    description="Chat Agent, you can chat with human",
    instruction="You are a help assistant. " "You can chat with human freely. ",
)

from google.adk.sessions import InMemorySessionService
from google.adk.runners import Runner

session_service = InMemorySessionService()


APP_NAME = "chat_app"
USER_ID = "chat_user"
SESSION_ID = "chat_session"


session = session_service.create_session(
    app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID
)
print(f"Session created: App='{APP_NAME}', User='{USER_ID}', Session='{SESSION_ID}'")

runner = Runner(
    agent=chat_agent,
    app_name=APP_NAME,
    session_service=session_service,
)

await call_agent_async(
    "Hello, how are you?",
    runner=runner,
    user_id=USER_ID,
    session_id=SESSION_ID,
)

I have tried every possible combination for the words after endpoints as per your instructions, but none have worked.

Output:

ClientError                               Traceback (most recent call last)
Cell In[5], line 1
----> 1 await call_agent_async(
      2     "Hello, how are you?",
      3     runner=runner,
      4     user_id=USER_ID,
      5     session_id=SESSION_ID,
      6 )

Cell In[1], line 8, in call_agent_async(query, runner, user_id, session_id)
      6 content = types.Content(role="user", parts=[types.Part(text=query)])
      7 final_response_text = "Agent did not produce a final response."
----> 8 async for event in runner.run_async(
      9     user_id=user_id, session_id=session_id, new_message=content
     10 ):
     11     if event.is_final_response():
     12         if event.content and event.content.parts:

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/adk/runners.py:197, in Runner.run_async(self, user_id, session_id, new_message, run_config)
    189   self._append_new_message_to_session(
    190       session,
    191       new_message,
    192       invocation_context,
    193       run_config.save_input_blobs_as_artifacts,
    194   )
    196 invocation_context.agent = self._find_agent_to_run(session, root_agent)
--> 197 async for event in invocation_context.agent.run_async(invocation_context):
    198   if not event.partial:
    199     self.session_service.append_event(session=session, event=event)

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/adk/agents/base_agent.py:141, in BaseAgent.run_async(self, parent_context)
    138 if ctx.end_invocation:
    139   return
--> 141 async for event in self._run_async_impl(ctx):
    142   yield event
    144 if ctx.end_invocation:

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/adk/agents/llm_agent.py:232, in LlmAgent._run_async_impl(self, ctx)
    228 @override
    229 async def _run_async_impl(
    230     self, ctx: InvocationContext
    231 ) -> AsyncGenerator[Event, None]:
--> 232   async for event in self._llm_flow.run_async(ctx):
    233     self.__maybe_save_output_to_state(event)
    234     yield event

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/adk/flows/llm_flows/base_llm_flow.py:231, in BaseLlmFlow.run_async(self, invocation_context)
    229 while True:
    230   last_event = None
--> 231   async for event in self._run_one_step_async(invocation_context):
    232     last_event = event
    233     yield event

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/adk/flows/llm_flows/base_llm_flow.py:257, in BaseLlmFlow._run_one_step_async(self, invocation_context)
    250 # Calls the LLM.
    251 model_response_event = Event(
    252     id=Event.new_id(),
    253     invocation_id=invocation_context.invocation_id,
    254     author=invocation_context.agent.name,
    255     branch=invocation_context.branch,
    256 )
--> 257 async for llm_response in self._call_llm_async(
    258     invocation_context, llm_request, model_response_event
    259 ):
    260   # Postprocess after calling the LLM.
    261   async for event in self._postprocess_async(
    262       invocation_context, llm_request, llm_response, model_response_event
    263   ):
    264     yield event

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/adk/flows/llm_flows/base_llm_flow.py:470, in BaseLlmFlow._call_llm_async(self, invocation_context, llm_request, model_response_event)
    465 else:
    466   # Check if we can make this llm call or not. If the current call pushes
    467   # the counter beyond the max set value, then the execution is stopped
    468   # right here, and exception is thrown.
    469   invocation_context.increment_llm_call_count()
--> 470   async for llm_response in llm.generate_content_async(
    471       llm_request,
    472       stream=invocation_context.run_config.streaming_mode
    473       == StreamingMode.SSE,
    474   ):
    475     trace_call_llm(
    476         invocation_context,
    477         model_response_event.id,
    478         llm_request,
    479         llm_response,
    480     )
    481     # Runs after_model_callback if it exists.

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/adk/models/google_llm.py:140, in Gemini.generate_content_async(self, llm_request, stream)
    133     yield LlmResponse(
    134         content=types.ModelContent(
    135             parts=[types.Part.from_text(text=text)],
    136         ),
    137     )
    139 else:
--> 140   response = await self.api_client.aio.models.generate_content(
    141       model=llm_request.model,
    142       contents=llm_request.contents,
    143       config=llm_request.config,
    144   )
    145   logger.info(_build_response_log(response))
    146   yield LlmResponse.create(response)

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/genai/models.py:6429, in AsyncModels.generate_content(self, model, contents, config)
   6427 response = types.GenerateContentResponse()
   6428 while remaining_remote_calls_afc > 0:
-> 6429   response = await self._generate_content(
   6430       model=model, contents=contents, config=config
   6431   )
   6432   remaining_remote_calls_afc -= 1
   6433   if remaining_remote_calls_afc == 0:

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/genai/models.py:5431, in AsyncModels._generate_content(self, model, contents, config)
   5428 request_dict = _common.convert_to_dict(request_dict)
   5429 request_dict = _common.encode_unserializable_types(request_dict)
-> 5431 response_dict = await self._api_client.async_request(
   5432     'post', path, request_dict, http_options
   5433 )
   5435 if self._api_client.vertexai:
   5436   response_dict = _GenerateContentResponse_from_vertex(
   5437       self._api_client, response_dict
   5438   )

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/genai/_api_client.py:708, in BaseApiClient.async_request(self, http_method, path, request_dict, http_options)
    697 async def async_request(
    698     self,
    699     http_method: str,
   (...)    702     http_options: Optional[HttpOptionsOrDict] = None,
    703 ) -> dict[str, object]:
    704   http_request = self._build_request(
    705       http_method, path, request_dict, http_options
    706   )
--> 708   result = await self._async_request(http_request=http_request, stream=False)
    709   json_response = result.json
    710   if not json_response:

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/genai/_api_client.py:652, in BaseApiClient._async_request(self, http_request, stream)
    644 else:
    645   response = await self._async_httpx_client.request(
    646       method=http_request.method,
    647       url=http_request.url,
   (...)    650       timeout=http_request.timeout,
    651   )
--> 652   await errors.APIError.raise_for_async_response(response)
    653   return HttpResponse(
    654       response.headers, response if stream else [response.text]
    655   )

File /opt/homebrew/anaconda3/envs/core-agent/lib/python3.12/site-packages/google/genai/errors.py:129, in APIError.raise_for_async_response(cls, response)
    127 status_code = response.status_code
    128 if 400 <= status_code < 500:
--> 129   raise ClientError(status_code, response_json, response)
    130 elif 500 <= status_code < 600:
    131   raise ServerError(status_code, response_json, response)

ClientError: 404 Not Found. {'message': '', 'status': 'Not Found'}

Could you please help me?

XinyueZ avatar May 01 '25 15:05 XinyueZ

@genquan9 Is this working now?

hangfei avatar May 31 '25 16:05 hangfei

There are some issues in tool callings when supporting llama4 currently.

genquan9 avatar Jun 04 '25 21:06 genquan9

llama4 models output tool calls as <|python_start|> ... <|python_end|>, which could not be supported for now. If you are interested, you can refer to some examples in https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_integration_with_agent.ipynb to get function calls and run function calls properly for llama4 models.

genquan9 avatar Jun 05 '25 20:06 genquan9