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

`llm_request.model` not respected when mixing model providers due to agent-based LLM resolution

Open triangle-z opened this issue 1 year ago • 4 comments

I'm implementing a feature where the same agent can serve different LLM providers dynamically based on user input. My solution is modifying the llm_request.model in a before_model callback depending on user choice, which works fine when all models come from the same provider (e.g., Gemini-only).

Example of my working callback:

def update_config(callback_context: CallbackContext, llm_request: LlmRequest) -> Optional[LlmResponse]:
    new_model = get_user_choice_model()
    if new_model:
        llm_request.model = new_model

This works because generate_content_async uses llm_request.model, so the response comes from the dynamically selected model.

However, when models from different providers (e.g., Gemini and Claude) are mixed, the agent still uses the default model's provider because in BaseLlmFlow._call_llm_async and BaseLlmFlow.run_live, the LLM is resolved like this:

llm = self.__get_llm(invocation_context)  # <- uses agent.model, not llm_request.model

This results in an error when the llm_request.model is from a different provider than the one the agent was originally instantiated with.

Temporary workaround (not safe for production):

I tried also changing the agent's model at runtime in the callback:

cast(LlmAgent, callback_context._invocation_context.agent).model = new_model

But this leads to shared-state issues when the same agent is invoked concurrently with different model selections (e.g., race conditions between users).


Proposed solution:

To respect the dynamically set llm_request.model, I suggest replacing this line:

llm = self.__get_llm(invocation_context)

with something like:

llm = LLMRegistry.new_llm(llm_request.model)

This ensures that the LLM provider is resolved based on the actual request and not the static agent configuration, enabling safe multi-user, multi-provider support without side effects.


💡 Additional note:

Since the model is no longer necessarily linked to the agent itself, it might be useful to store the actual model used (from llm_request.model) in the events/logs for traceability. This will help with debugging, analytics, and audit trails.

triangle-z avatar Jun 03 '25 11:06 triangle-z

Of course, it’s not that simple 😅

I noticed another detail in _BasicLlmRequestProcessor:

llm_request.model = (
    agent.canonical_model
    if isinstance(agent.canonical_model, str)
    else agent.canonical_model.model
)

So llm_request.model ends up being just a string, even if the agent’s canonical_model is an instantiated model object. That’s not technically a problem since LLMRegistry.new_llm(llm_request.model) expects a string anyway, but it made me wonder:

Is there a conceptual distinction between agent.model and llm_request.model?

My understanding (and goal) is:

  • agent.model should hold the default model (static, configured per agent)
  • llm_request.model should allow a runtime override (e.g. via a before_model callback)

Are they currently redundant, or is a separation of concerns already intended in the design ? Just want to confirm I’m aligned with the framework’s direction.

triangle-z avatar Jun 03 '25 23:06 triangle-z

What is your goal? It is possible to add a list of agents (with different providers) to root_agent and instruct root agent. root_agent will pass control to the required agent and the selected agent will respond to the user. It should be noted here that it is the list of agents that should be added, not the AgentTool in tools

NikitaZemtsov avatar Jun 05 '25 16:06 NikitaZemtsov

@NikitaZemtsov thanks for your comment. I want the users to be able to choose which model they want to get an answer from. One option is to create a separate agent for each model, but that would lead to code duplication since the use cases, tools, ... remain the same. Your workaround would indeed work, I didn’t think about it, but it would add an additional LLM call for each request, which could be avoided if dynamic model selection was possible.

triangle-z avatar Jun 05 '25 18:06 triangle-z

Hi @triangle-z 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!

klateefa avatar Oct 08 '25 05:10 klateefa