# Conversation state

Learn how to manage conversation state during a model interaction.

OpenAI provides a few ways to manage conversation state, which is important for
preserving information across multiple messages or turns in a conversation.

## Manually manage conversation state

While each text generation request is independent and stateless, you can still
implement **multi-turn conversations** by providing additional messages as
parameters to your text generation request. Consider a knock-knock joke:

```javascript
import OpenAI from "openai";

const openai = new OpenAI();

const response = await openai.responses.create({
  model: "gpt-4o-mini",
  input: [
    { role: "user", content: "knock knock." },
    { role: "assistant", content: "Who's there?" },
    { role: "user", content: "Orange." },
  ],
});

console.log(response.output_text);
```

```python
from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="gpt-4o-mini",
    input=[
        {"role": "user", "content": "knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
    ],
)

print(response.output_text)
```

By using alternating `user` and `assistant` messages, you capture the previous
state of a conversation in one request to the model.

To manually share context across generated responses, include the model's
previous response output as input, and append that input to your next request.

In the following example, we ask the model to tell a joke, followed by a request
for another joke. Appending previous responses to new requests in this way helps
ensure conversations feel natural and retain the context of previous
interactions.

```javascript
import OpenAI from "openai";

const openai = new OpenAI();

let history = [
  {
    role: "user",
    content: "tell me a joke",
  },
];

const response = await openai.responses.create({
  model: "gpt-4o-mini",
  input: history,
  store: true,
});

console.log(response.output_text);

// Add the response to the history
history = [
  ...history,
  ...response.output.map((el) => {
    // TODO: Remove this step
    delete el.id;
    return el;
  }),
];

history.push({
  role: "user",
  content: "tell me another",
});

const secondResponse = await openai.responses.create({
  model: "gpt-4o-mini",
  input: history,
  store: true,
});

console.log(secondResponse.output_text);
```

```python
from openai import OpenAI

client = OpenAI()

history = [
    {
        "role": "user",
        "content": "tell me a joke"
    }
]

response = client.responses.create(
    model="gpt-4o-mini",
    input=history,
    store=False
)

print(response.output_text)