# Learning=True Shorthand Test (/examples/learning/quick-tests/learning-true-shorthand)



```python title="learning_true_shorthand.py"
"""
Learning=True Shorthand Test
============================
Tests the simplest way to enable learning: `learning=True`.

This is the most common user pattern and must work flawlessly.

When learning=True:
- A default LearningMachine is created
- UserProfile is enabled with ALWAYS mode (structured fields)
- UserMemory is enabled with ALWAYS mode (unstructured observations)
- db and model are injected from the agent

This test verifies the shorthand works identically to explicit config.
"""

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.openai import OpenAIResponses

# ---------------------------------------------------------------------------
# Create Agent - Using the simplest possible configuration
# ---------------------------------------------------------------------------

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

# This is the simplest way to enable learning - just set learning=True
agent = Agent(
    model=OpenAIResponses(id="gpt-5.5"),
    db=db,
    learning=True,  # <-- The shorthand we're testing
    markdown=True,
)

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

if __name__ == "__main__":
    user_id = "shorthand_test@example.com"

    # Note: LearningMachine is lazily initialized - only set up when agent runs
    print("\n" + "=" * 60)
    print("SESSION 1: Share information (learning=True shorthand)")
    print("=" * 60 + "\n")

    agent.print_response(
        "Hi! I'm Charlie Brown. Friends call me Chuck.",
        user_id=user_id,
        session_id="shorthand_session_1",
        stream=True,
    )

    # Verify LearningMachine was created (after first run)
    print("\n" + "=" * 60)
    print("VERIFICATION: LearningMachine created from learning=True")
    print("=" * 60 + "\n")

    lm = agent.learning_machine
    print(f"LearningMachine exists: {lm is not None}")
    print(
        f"UserProfileStore exists: {lm.user_profile_store is not None if lm else False}"
    )
    print(
        f"UserMemoryStore exists: {lm.user_memory_store is not None if lm else False}"
    )
    print(f"DB injected: {lm.db is not None if lm else False}")
    print(f"Model injected: {lm.model is not None if lm else False}")

    if not lm:
        print("\nFAILED: LearningMachine was not created!")
        exit(1)

    if not lm.user_profile_store:
        print("\nFAILED: UserProfileStore was not created!")
        exit(1)

    if not lm.user_memory_store:
        print("\nFAILED: UserMemoryStore was not created!")
        exit(1)

    print("\n--- User Profile ---")
    lm.user_profile_store.print(user_id=user_id)

    print("\n--- User Memory ---")
    lm.user_memory_store.print(user_id=user_id)

    # Session 2: Verify profile persisted
    print("\n" + "=" * 60)
    print("SESSION 2: Profile recall")
    print("=" * 60 + "\n")

    agent.print_response(
        "What do my friends call me?",
        user_id=user_id,
        session_id="shorthand_session_2",
        stream=True,
    )

    print("\n--- User Profile ---")
    lm.user_profile_store.print(user_id=user_id)

    print("\n--- User Memory ---")
    lm.user_memory_store.print(user_id=user_id)

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

## Run the Example [#run-the-example]

<Steps>
    <Step title="Set up your virtual environment">
      <CodeBlockTabs defaultValue="Mac">
        <CodeBlockTabsList>
          <CodeBlockTabsTrigger value="Mac">
            Mac
          </CodeBlockTabsTrigger>

          <CodeBlockTabsTrigger value="Windows">
            Windows
          </CodeBlockTabsTrigger>
        </CodeBlockTabsList>

        <CodeBlockTab value="Mac">
          ```bash
          uv venv --python 3.12
          source .venv/bin/activate
          ```
        </CodeBlockTab>

        <CodeBlockTab value="Windows">
          ```bash
          uv venv --python 3.12
          .venv\Scripts\activate
          ```
        </CodeBlockTab>
      </CodeBlockTabs>
    </Step>

  <Step title="Install dependencies">
    ```bash
    uv pip install -U agno "psycopg[binary]" openai sqlalchemy
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeBlockTabs defaultValue="Mac/Linux">
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="Mac/Linux">
          Mac/Linux
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="Windows">
          Windows
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="Mac/Linux">
        ```bash
        export OPENAI_API_KEY="your_openai_api_key_here"
        ```
      </CodeBlockTab>

      <CodeBlockTab value="Windows">
        ```bash
        $Env:OPENAI_API_KEY="your_openai_api_key_here"
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </Step>

    <Step title="Run PgVector">
      <CodeBlockTabs defaultValue="macOS / Linux">
        <CodeBlockTabsList>
          <CodeBlockTabsTrigger value="macOS / Linux">
            macOS / Linux
          </CodeBlockTabsTrigger>

          <CodeBlockTabsTrigger value="Windows">
            Windows
          </CodeBlockTabsTrigger>
        </CodeBlockTabsList>

        <CodeBlockTab value="macOS / Linux">
          ```bash
          docker run -d \
            -e POSTGRES_DB=ai \
            -e POSTGRES_USER=ai \
            -e POSTGRES_PASSWORD=ai \
            -e PGDATA=/var/lib/postgresql \
            -v pgvolume:/var/lib/postgresql \
            -p 5532:5432 \
            --name pgvector \
            agnohq/pgvector:18
          ```
        </CodeBlockTab>

        <CodeBlockTab value="Windows">
          ```powershell
          docker run -d `
            -e POSTGRES_DB=ai `
            -e POSTGRES_USER=ai `
            -e POSTGRES_PASSWORD=ai `
            -e PGDATA=/var/lib/postgresql `
            -v pgvolume:/var/lib/postgresql `
            -p 5532:5432 `
            --name pgvector `
            agnohq/pgvector:18
          ```
        </CodeBlockTab>
      </CodeBlockTabs>
    </Step>

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

    ```bash
    python learning_true_shorthand.py
    ```
  </Step>
</Steps>

Full source: [cookbook/08\_learning/06\_quick\_tests/02\_learning\_true\_shorthand.py](https://github.com/agno-agi/agno/blob/8f36eaf2d18e91afa7b327eec66a3cd3685dcb87/cookbook/08_learning/06_quick_tests/02_learning_true_shorthand.py)
