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

# OpenAI Python SDK

> Use the OpenAI Python SDK with FriendliAI endpoints. Migrate existing Python apps by changing the base URL. Covers chat, streaming, and tool calling.

You can use [**OpenAI Python SDK**](https://github.com/openai/openai-python) to interact with FriendliAI.
This makes migration of existing applications already using OpenAI particularly easy.

## How to Use

Before you start, ensure the `base_url` and `api_key` refer to FriendliAI.
FriendliAI is fully compatible with the OpenAI SDK, so you can follow the examples below.

Choose one of the [available models](https://friendli.ai/models?products=SERVERLESS) for the `model` parameter.

```bash theme={null}
pip install -qU openai
```

### Chat Completion

Chat completion API that generates a response from a given conversation.

Choose the example that best fits your needs:

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

  client = OpenAI(
      base_url="https://api.friendli.ai/serverless/v1",
      api_key=os.environ.get("API_KEY")
  )

  completion = client.chat.completions.create(
      model="zai-org/GLM-5.2",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"}
      ]
  )

  print(completion.choices[0].message)
  ```

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

  client = OpenAI(
      base_url="https://api.friendli.ai/serverless/v1",
      api_key=os.environ.get("API_KEY")
  )

  completion = client.chat.completions.create(
      model="zai-org/GLM-5.2",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"}
      ],
      stream=True
  )

  for chunk in completion:
      print(chunk.choices[0].delta)
  ```

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

  client = OpenAI(
      base_url="https://api.friendli.ai/serverless/v1",
      api_key=os.environ.get("API_KEY")
  )

  tools = [
      {
          "type": "function",
          "function": {
              "name": "get_current_weather",
              "description": "Get the current weather in a given location",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "location": {
                          "type": "string",
                          "description": "The city and state, e.g. San Francisco, CA",
                      },
                      "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                  },
                  "required": ["location"],
              },
          }
      }
  ]

  completion = client.chat.completions.create(
      model="zai-org/GLM-5.2",
      messages=[
          {"role": "user", "content": "What's the weather like in Boston today?"}
      ],
      tools=tools,
      tool_choice="auto"
  )

  print(completion)
  ```

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

  client = OpenAI(
      base_url="https://api.friendli.ai/serverless/v1",
      api_key=os.environ.get("API_KEY")
  )

  completion = client.chat.completions.create(
      model="zai-org/GLM-5.2",
      messages=[
          {"role": "user", "content": "Hello!"}
      ],
      logprobs=True,
      top_logprobs=2
  )

  print(completion.choices[0].message)
  print(completion.choices[0].logprobs)
  ```
</CodeGroup>
