> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agno.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Chat Responses

> Alternate one agent between OpenAIChat and OpenAIResponses mid-session over shared Postgres history.

```python openai_chat_responses.py theme={null}
import os

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


def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"The weather in {city} is sunny and 22C."


def main() -> None:
    db_url = os.getenv(
        "AGNO_POSTGRES_URL",
        "postgresql+psycopg://ai:ai@localhost:5532/ai",
    )
    db = PostgresDb(db_url)

    agent = Agent(
        model=OpenAIChat(id="gpt-4o"),
        db=db,
        add_history_to_context=True,
        num_history_runs=10,
        tools=[get_weather],
        debug_mode=True,
    )

    # Turn 1 — OpenAI with tool call (works fine)
    agent.print_response("What is the weather in Paris?")

    # Turn 2 — OpenAI Responses with tool call
    agent.model = OpenAIResponses()
    agent.print_response("What is the weather in London?")

    # Turn 3 — OpenAI with tool call (works fine on its own)
    agent.model = OpenAIChat(id="gpt-4o")
    agent.print_response("What is the weather in Tokyo?")

    # Turn 4 — OpenAI Responses summary
    agent.model = OpenAIResponses()
    agent.print_response("Summarize all the weather we checked.")


if __name__ == "__main__":
    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 `openai_chat_responses.py`, then run:

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

Full source: [cookbook/02\_agents/14\_advanced/interchange\_model/openai\_chat\_responses.py](https://github.com/agno-agi/agno/blob/main/cookbook/02_agents/14_advanced/interchange_model/openai_chat_responses.py)
