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

# Python Basic Chat

> Python basic chat, multi-turn conversation, and streaming output

> Last updated: 2026-06-06

## Install Dependencies

```bash theme={null}
pip install openai requests
```

## Basic Chat

<CodeGroup>
  ```python OpenAI SDK theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="sk-xxx",
      base_url="https://api.crazyrouter.com/v1"
  )

  response = client.chat.completions.create(
      model="gpt-5.5",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Explain artificial intelligence in one sentence"}
      ]
  )

  print(response.choices[0].message.content)
  ```

  ```python requests theme={null}
  import requests

  response = requests.post(
      "https://api.crazyrouter.com/v1/chat/completions",
      headers={
          "Authorization": "Bearer sk-xxx",
          "Content-Type": "application/json"
      },
      json={
          "model": "gpt-5.5",
          "messages": [
              {"role": "user", "content": "Explain artificial intelligence in one sentence"}
          ]
      }
  )

  data = response.json()
  print(data["choices"][0]["message"]["content"])
  ```
</CodeGroup>

## Multi-turn Conversation

Maintain a `messages` list to implement multi-turn dialogue:

```python theme={null}
from openai import OpenAI

client = OpenAI(api_key="sk-xxx", base_url="https://api.crazyrouter.com/v1")

messages = [
    {"role": "system", "content": "You are a Python programming assistant."}
]

def chat(user_input):
    messages.append({"role": "user", "content": user_input})

    response = client.chat.completions.create(
        model="gpt-5.5",
        messages=messages
    )

    reply = response.choices[0].message.content
    messages.append({"role": "assistant", "content": reply})
    return reply

# Multi-turn dialogue
print(chat("How do I read a CSV file?"))
print(chat("What if the file is very large?"))
print(chat("Can I use pandas for this?"))
```

## Streaming Output

```python theme={null}
from openai import OpenAI

client = OpenAI(api_key="sk-xxx", base_url="https://api.crazyrouter.com/v1")

stream = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "Write a poem about programming"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
print()
```

## Streaming with requests

```python theme={null}
import requests
import json

response = requests.post(
    "https://api.crazyrouter.com/v1/chat/completions",
    headers={
        "Authorization": "Bearer sk-xxx",
        "Content-Type": "application/json"
    },
    json={
        "model": "gpt-5.5",
        "messages": [{"role": "user", "content": "Hello"}],
        "stream": True
    },
    stream=True
)

for line in response.iter_lines():
    if line:
        line = line.decode("utf-8")
        if line.startswith("data: ") and line != "data: [DONE]":
            data = json.loads(line[6:])
            content = data["choices"][0]["delta"].get("content", "")
            print(content, end="", flush=True)
print()
```

<Note>
  All examples default to `gpt-5.5`, which was verified successfully in Crazyrouter production on March 23, 2026. If you need to swap models, prefer other verified options such as `claude-opus-4-8` or `gemini-3.1-pro`.
</Note>
