> ## 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.

# Friendli Python SDK

> Install and use the Friendli Python SDK to access Model APIs, Dedicated Endpoints, and Container. Covers chat, completions, streaming, and async usage.

## Introduction

The [Friendli Python SDK](https://github.com/friendliai/friendli-python) provides a powerful and flexible way to interact with FriendliAI services, including Model APIs, Dedicated Endpoints, and Container. You can easily integrate your Python applications with FriendliAI.

## Installation

You can install the SDK with either pip or poetry:

```bash theme={null}
# Using pip
pip install friendli

# Using poetry
poetry add friendli
```

## Authentication

Authenticate using a Personal API key, which you can generate from [Friendli Suite > Personal Settings > API Keys](https://friendli.ai/suite/~/setting/keys):

```python theme={null}
import os
from friendli import SyncFriendli

with SyncFriendli(
    token=os.environ["API_KEY"],
) as friendli:
    # Your code here
```

For detailed instructions on generating an API key, see the [Manage Your FriendliAI API Keys](/docs/guides/suite/personal-api-keys) guide.

## Chat Completions

The SDK supports chat completions across all deployment types. Choose the deployment option that best fits your needs.

<CodeGroup>
  ```python Model APIs theme={null}
  import os
  from friendli import SyncFriendli

  with SyncFriendli(
      token=os.environ["API_KEY"],
  ) as friendli:
      res = friendli.serverless.chat.complete(
          messages=[
              {
                  "content": "You are a helpful assistant.",
                  "role": "system",
              },
              {
                  "content": "Hello!",
                  "role": "user",
              },
          ],
          model="zai-org/GLM-5.3",
          max_tokens=200,
      )

      print(res)
  ```

  ```python Dedicated Endpoints theme={null}
  import os
  from friendli import SyncFriendli

  with SyncFriendli(
      token=os.environ["API_KEY"],
  ) as friendli:
      res = friendli.dedicated.chat.complete(
          messages=[
              {
                  "content": "You are a helpful assistant.",
                  "role": "system",
              },
              {
                  "content": "Hello!",
                  "role": "user",
              },
          ],
          model="YOUR_ENDPOINT_ID",
          max_tokens=200,
      )

      print(res)
  ```

  ```python Container Deployment theme={null}
  from friendli import SyncFriendli

  with SyncFriendli() as friendli:
      res = friendli.container.chat.complete(
          messages=[
              {
                  "content": "You are a helpful assistant.",
                  "role": "system",
              },
              {
                  "content": "Hello!",
                  "role": "user",
              },
          ],
          max_tokens=200,
      )

      print(res)
  ```
</CodeGroup>

### Asynchronous Chat Completions

<CodeGroup>
  ```python Model APIs theme={null}
  import asyncio
  import os
  from friendli import AsyncFriendli

  async def main():
      async with AsyncFriendli(
          token=os.environ["API_KEY"],
      ) as friendli:
          res = await friendli.serverless.chat.complete(
              messages=[
                  {
                      "content": "You are a helpful assistant.",
                      "role": "system",
                  },
                  {
                      "content": "Hello!",
                      "role": "user",
                  },
              ],
              model="zai-org/GLM-5.3",
              max_tokens=200,
          )

          print(res)

  asyncio.run(main())
  ```

  ```python Dedicated Endpoints theme={null}
  import asyncio
  import os
  from friendli import AsyncFriendli

  async def main():
      async with AsyncFriendli(
          token=os.environ["API_KEY"],
      ) as friendli:
          res = await friendli.dedicated.chat.complete(
              messages=[
                  {
                      "content": "You are a helpful assistant.",
                      "role": "system",
                  },
                  {
                      "content": "Hello!",
                      "role": "user",
                  },
              ],
              model="YOUR_ENDPOINT_ID",
              max_tokens=200,
          )

          print(res)

  asyncio.run(main())
  ```

  ```python Container Deployment theme={null}
  import asyncio
  from friendli import AsyncFriendli

  async def main():
      async with AsyncFriendli() as friendli:
          res = await friendli.container.chat.complete(
              messages=[
                  {
                      "content": "You are a helpful assistant.",
                      "role": "system",
                  },
                  {
                      "content": "Hello!",
                      "role": "user",
                  },
              ],
              max_tokens=200,
          )

          print(res)

  asyncio.run(main())
  ```
</CodeGroup>

## Advanced Features

### Streaming Responses

The SDK supports streaming responses using server-sent events, which you can consume using a simple `for` loop:

```python theme={null}
import os
from friendli import SyncFriendli

with SyncFriendli(
    token=os.environ["API_KEY"],
) as friendli:
    res = friendli.serverless.chat.stream(
        messages=[
            {
                "content": "You are a helpful assistant.",
                "role": "system",
            },
            {
                "content": "Hello!",
                "role": "user",
            },
        ],
        model="zai-org/GLM-5.3",
        max_tokens=200,
    )

    with res as event_stream:
        for event in event_stream:
            # Process each chunk as it arrives
            print(event, flush=True)
```

### Custom Retry Strategy

You can customize retry behavior for operations that support retries:

```python theme={null}
import os
from friendli import SyncFriendli
from friendli.utils import BackoffStrategy, RetryConfig

with SyncFriendli(
    token=os.environ["API_KEY"],
) as friendli:
    res = friendli.serverless.chat.complete(
        messages=[
            {
                "content": "You are a helpful assistant.",
                "role": "system",
            },
            {
                "content": "Hello!",
                "role": "user",
            },
        ],
        model="zai-org/GLM-5.3",
        max_tokens=200,
        retries=RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False),
    )

    # Handle response
    print(res)
```

### Error Handling

The SDK provides comprehensive error handling with detailed exception information:

```python theme={null}
import os
from friendli import SyncFriendli, models

with SyncFriendli(
    token=os.environ["API_KEY"],
) as friendli:
    try:
        res = friendli.dedicated.endpoint.create(
            advanced={
                "tokenizer_add_special_tokens": True,
                "tokenizer_skip_special_tokens": False,
            },
            hf_model_repo="<value>",
            instance_option_id="<id>",
            name="<value>",
            project_id="<id>",
        )

        # Handle response
        print(res)

    except models.HTTPValidationError as e:
        # Handle validation errors
        print(f"Validation error: {e.data}")
    except models.SDKError as e:
        # Handle general SDK errors
        print(f"Error {e.status_code}: {e.message}")
```

### Custom Logging

You can pass your own logger to the client class to help troubleshoot and diagnose issues during API interactions. This is especially useful when you encounter unexpected behavior or errors.

```python theme={null}
import logging
import os

from friendli import SyncFriendli

# Configure your custom logger, for example:
logger = logging.getLogger(__name__)
logging.basicConfig(
    format="[%(filename)s:%(lineno)s - %(funcName)s()] %(message)s",
    level=logging.INFO,
    handlers=[logging.StreamHandler()],
)

with SyncFriendli(
    server_url=SERVER_URL,
    token=TOKEN,
    debug_logger=logger,  # Pass your logger here
) as friendli:
    # Your code here
    pass
```

## Further Resources

For complete API documentation, advanced usage examples, and detailed reference information, visit the [Friendli Python SDK GitHub repository](https://github.com/friendliai/friendli-python).
