> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-service-account.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Async User Profile Test

> Tests the async path for user profile learning.

```python async_user_profile.py theme={null}
"""
Async User Profile Test
=======================
Tests the async path for user profile learning.

All other cookbooks use sync (print_response). This test verifies
that the async path (aprint_response) works correctly.

This is critical because:
- Background learning uses asyncio tasks in async mode
- Different code paths for aprocess vs process
- Potential race conditions in async context
"""

import asyncio

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.learn import LearningMachine, LearningMode, UserProfileConfig
from agno.models.openai import OpenAIResponses

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")

agent = Agent(
    model=OpenAIResponses(id="gpt-5.5"),
    db=db,
    learning=LearningMachine(
        user_profile=UserProfileConfig(
            mode=LearningMode.ALWAYS,
        ),
    ),
    markdown=True,
)

# ---------------------------------------------------------------------------
# Run Async Demo
# ---------------------------------------------------------------------------


async def main():
    user_id = "async_test@example.com"

    # Session 1: Share information (async)
    print("\n" + "=" * 60)
    print("SESSION 1: Async - Share information")
    print("=" * 60 + "\n")

    await agent.aprint_response(
        "Hi! I'm Diana Prince, but call me Di.",
        user_id=user_id,
        session_id="async_session_1",
        stream=True,
    )
    agent.learning_machine.user_profile_store.print(user_id=user_id)

    # Session 2: New session - verify profile persisted (async)
    print("\n" + "=" * 60)
    print("SESSION 2: Async - Profile recall")
    print("=" * 60 + "\n")

    await agent.aprint_response(
        "What's my name?",
        user_id=user_id,
        session_id="async_session_2",
        stream=True,
    )
    agent.learning_machine.user_profile_store.print(user_id=user_id)

    print("\n" + "=" * 60)
    print("ASYNC TEST COMPLETE")
    print("=" * 60)


if __name__ == "__main__":
    asyncio.run(main())
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno openai psycopg-binary sqlalchemy
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Snippet file="run-pgvector-step.mdx" />

  <Step title="Run the example">
    Save the code above as `async_user_profile.py`, then run:

    ```bash theme={null}
    python async_user_profile.py
    ```
  </Step>
</Steps>

Full source: [cookbook/08\_learning/06\_quick\_tests/01\_async\_user\_profile.py](https://github.com/agno-agi/agno/blob/main/cookbook/08_learning/06_quick_tests/01_async_user_profile.py)
