Skip to content

before_agent_callback Fails to Read existing Session Context Variables #3223

@carlada-ita

Description

@carlada-ita

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

Describe the bug
I created an application using the ADK agents, deployed on Cloud Run. I call the Cloud Run service from a Linux server using the CURL commands:

  • Session creation:
    curl -X POST -H "Authorization: Bearer $TOKEN"
    $APP_URL/apps/app_agent/users/user_234/sessions/session_234
    -H "Content-Type: application/json"
    -d '{"state": {"nome_doc": "document.pdf", "tag": "1_0_0"}}'

  • Agent invocation:
    curl -X POST -H "Authorization: Bearer $TOKEN"
    $APP_URL/run_sse
    -H "Content-Type: application/json"
    -d '{
    "app_name": "app_agent",
    "user_id": "user_234",
    "session_id": "session_234",
    "new_message": {
    "role": "user",
    "parts": [{
    "text": "GO!"
    }]
    },
    "streaming": false
    }'

The agent application, when attempting to read the context variables passed during session creation, cannot find them, even if both variables as present and correctly set in the STATE (see logging below).

To Reproduce

  • Agent definition
    analysis_pipeline = SequentialAgent(
    name="analysis_pipeline",
    description="",
    sub_agents=[
    recognizer_doc,
    analysis_loop,
    report_composer,
    ],
    before_agent_callback=pipeline_init_context,
    )

  • before_agent_callback function
    async def pipeline_init_context(callback_context: CallbackContext):
    """
    Callback function (callback_before_agent) for loading the context variables of the AGENTS.

    Args:
    callback_context: session context on which the agents will work

    Returns:
    None
    """

    logging.info(f"\n*---------------------- function pipeline_init_context")

    try:
    logging.info(f"{callback_context.state.to_dict()}")

      nome_doc = callback_context.state.get("nome_doc")
      tag = callback_context.state.get("tag")
      logging.info(f"*---------  state: nome_doc = {nome_doc}")
      logging.info(f"*---------  state: tag = {tag}")
    
      nome_doc1 = callback_context.state["nome_doc"]  # generate application error 
      tag1 = callback_context.state["tag"]
      logging.info(f"*---------  state: nome_doc = {nome_doc1}")
      logging.info(f"*---------  state: tag = {tag1}")
    
      #callback_context.state["tagmod"] = tag.replace("_", ".")
    
      logging.info(f"*---------  state: tag = {tag}")
      logging.info(f"*---------  state: tagmod = {callback_context.state["tagmod"]}")
      logging.info(f"*---------  state: nome_doc = {nome_doc}")
      .....
    

    .....
    .....
    except Exception as e:
    logging.error(f"*--------- status: error, message: No document found or loaded. {e}")
    return None

Expected behavior
Correct loading of the context variables.

Screenshots

******** OUTPUT ********
$ curl -X POST -H "Authorization: Bearer $TOKEN"
$APP_URL/apps/app_agent/users/user_234/sessions/session_234
-H "Content-Type: application/json"
-d '{"state": {"nome_doc": "document.pdf", "tag": "1_0_0"}}'

response:
{"id":"session_234","appName":"app_agent","userId":"user_234","state":{"state":{"nome_doc":"document.pdf","tag":"1_0_0"}},"events":[],"lastUpdateTime":1760876600.5299006}

$ curl -X GET -H "Authorization: Bearer $TOKEN"
$APP_URL/apps/app_agent/users/user_234/sessions/session_234

response.
{"id":"session_234","appName":"app_agent","userId":"user_234","state":{"state":{"nome_doc":"document.pdf","tag":"1_0_0"}},"events":[],"lastUpdateTime":1760876600.5299006}

$ curl -X POST -H "Authorization: Bearer $TOKEN"
$APP_URL/run_sse
-H "Content-Type: application/json"
-d '{
"app_name": "app_agent",
"user_id": "user_234",
"session_id": "session_234",
"new_message": {
"role": "user",
"parts": [{
"text": "GO!"
}]
},
"streaming": false
}'

response:
data: {"error": "'Context variable not found: nome_doc.'"}

******** Application LOG ********
*---------------------- function pipeline_init_context
*--------- {'state': {'nome_doc': 'document.pdf', 'tag': '1_0_0'}}
*--------- state: nome_doc = None
*--------- state: tag = None
*--------- pre callback_context.state["nome_doc"]
*--------- status: error, message: No document found or loaded. 'nome_doc'

Desktop (please complete the following information):

  • Google ADK version: 1.5.0
  • Python version: 3.12
  • Framework: FastAPI with async orchestration

Model Information:

  • Are you using LiteLLM: No
  • Model is being used gemini-2.5-pro

Metadata

Metadata

Labels

core[Component] This issue is related to the core interface and implementation

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions