[Python] Python asyncio: AttributeError: 'coroutine' object has no attribute 'state' with...

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to run a basic stateful session using the google.adk library in Python, interacting with a question_answering_agent and InMemorySessionService. I encountered a series of errors related to imports, asynchronous operations, and API key loading.

    My goal is to successfully run the basic_stateful_session.py script which initializes a session, sends a message to an agent, and then logs the final session state.

    However, I get the following error:

    AttributeError: 'coroutine' object has no attribute 'state' and ValueError: Session not found (with RuntimeWarning: coroutine '...' was never awaited)


    The script: basic_stateful_session.py

    import uuid

    from dotenv import load_dotenv
    from google.adk.runners import Runner
    from google.adk.sessions import InMemorySessionService
    from google.genai import types
    from question_answering_agent import question_answering_agent

    load_dotenv()


    # Create a new session service to store state
    session_service_stateful = InMemorySessionService()

    initial_state = {
    "user_name": "Brandon Hancock",
    "user_preferences": """
    I like to play Pickleball, Disc Golf, and Tennis.
    My favorite food is Mexican.
    My favorite TV show is Game of Thrones.
    Loves it when people like and subscribe to his YouTube channel.
    """,
    }

    # Create a NEW session
    APP_NAME = "Brandon Bot"
    USER_ID = "brandon_hancock"
    SESSION_ID = str(uuid.uuid4())
    stateful_session = session_service_stateful.create_session(
    app_name=APP_NAME,
    user_id=USER_ID,
    session_id=SESSION_ID,
    state=initial_state,
    )
    print("CREATED NEW SESSION:")
    print(f"\tSession ID: {SESSION_ID}")

    runner = Runner(
    agent=question_answering_agent,
    app_name=APP_NAME,
    session_service=session_service_stateful,
    )

    new_message = types.Content(
    role="user", parts=[types.Part(text="What is Brandon's favorite TV show?")]
    )

    for event in runner.run(
    user_id=USER_ID,
    session_id=SESSION_ID,
    new_message=new_message,
    ):
    if event.is_final_response():
    if event.content and event.content.parts:
    print(f"Final Response: {event.content.parts[0].text}")

    print("==== Session Event Exploration ====")
    session = session_service_stateful.get_session(
    app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID
    )

    # Log final Session state
    print("=== Final Session State ===")
    for key, value in session.state.items():
    print(f"{key}: {value}")


    The agent:

    from google.adk.agents import Agent

    # Create the root agent
    question_answering_agent = Agent(
    name="question_answering_agent",
    model="gemini-2.0-flash",
    description="Question answering agent",
    instruction="""
    You are a helpful assistant that answers questions about the user's preferences.

    Here is some information about the user:
    Name:
    {user_name}
    Preferences:
    {user_preferences}
    """,
    )

    Continue reading...

Compartilhe esta Página