> This page is for SDKs, version Legacy.
> For other versions, use one of these documentation indexes:
> - Current (default): https://buildwithfern.com/learn/sdks/current/llms.txt
> - Legacy: https://buildwithfern.com/learn/sdks/legacy/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt.

# Adding custom code

> Learn how to add custom logic, methods, and dependencies to your Python SDK. Extend Fern-generated clients with custom code.

> **You're viewing the legacy SDK docs**
>
> These pages cover SDK generation configured with `generators.yml`. New projects use `sdk-config.yml`, so see [the Current version of this page](/learn/sdks/generators/python/custom-code) for up-to-date guidance. To move an existing project, follow the [SDK Config migration guide](/learn/sdks/legacy/generators/migration-guides/sdk-config), which walks through `fern sdk migrate`, and review the [`sdk-config.yml` reference](/learn/sdks/reference/sdk-config).

> **Enterprise feature**
>
> This feature is available only for the [Enterprise plan](https://buildwithfern.com/pricing). To get started, reach out to [support@buildwithfern.com](mailto:support@buildwithfern.com).

This page covers how to add custom logic, methods, and dependencies to your Python SDK.

## Adding custom logic

To get started adding custom code:

> **Note**
>
> These steps cover adding a new custom file to the SDK. To preserve line-level edits to a generated file, use [Replay](/learn/sdks/legacy/overview/custom-code#replay) instead.

### Create a new file and add your custom logic

**`src/<package>/helper.py`**

```python title="src/<package>/helper.py"
def my_helper() -> None:
  print("Hello World!")
```

### Add your file to `.fernignore`

**`.fernignore`**

```yaml {3} title=".fernignore"
# Specify files that shouldn't be modified by Fern

src/<package>/helper.py
```

### Consume the helper

Now your users can consume the helper function by importing it from the SDK.

```python
from package.helper import my_helper

my_helper()
```

## Adding custom SDK methods

Fern also allows you to add custom methods to the SDK itself (e.g.
`client.my_method()` ) by inheriting the Fern generated client and then
extending it.

> **Note**
>
> See an example from ElevenLabs using this process in their [Python SDK](https://github.com/elevenlabs/elevenlabs-python/blob/main/src/elevenlabs/client.py).

### Update `generators.yml` configuration

Name your Fern-generated client something like `BaseClient` to reflect
that this client will be extended. Configure the generator to output the
client in a file called `base_client.py`.

**`generators.yml`**

```yaml {4-8} title="generators.yml"
- name: fern-python-sdk
  version: "..."
  config:
    client:
      class_name: BaseClient        # The name of the generated client you will extend
      filename: base_client.py      # The name of the file the generated client will live in
      exported_class_name: YourClient   # The name of the class you will be creating that extends the generated client
      exported_filename: client.py
```

### Generate the SDK

Trigger SDK generation by running `fern generate`:

```bash
fern generate --group sdk
```

### Import and extend the generated client

First, import the Fern generated base clients from `.base_client.py` and extend them to create your custom clients. Then, add whatever methods you want.

**`src/<package>/client.py`**

```python title="src/<package>/client.py"
  from .base_client import BaseClient // import generated client
  
  class YourClient(BaseClient): // extend generated client
      def my_helper(self) -> None:
          print("Hello World!")
          def my_helper(self) -> None:
              print("Hello World")
```

> **Note**
>
> See an example [client.py](https://github.com/elevenlabs/elevenlabs-python/blob/main/src/elevenlabs/client.py) from ElevenLabs.

### Update `.fernignore`

Add the `client.py` to `.fernignore`.

**`.fernignore`**

```diff title=".fernignore"
+ src/<package>/client.py
```

> **Note**
>
> See an example [.fernignore](https://github.com/elevenlabs/elevenlabs-python/blob/main/.fernignore) from ElevenLabs.

### Consume the method

Now your users can consume the helper function by importing it from the SDK.

```python
client.my_helper()
```

## Adding custom dependencies

To add packages that your custom code requires, update your `generators.yml`.

**`generators.yml`**

```yaml {4-7} title="generators.yml"
- name: fern-python-sdk
  version: "..."
  config:
    extra_dependencies:
      numpy: '1.2.0'
    extra_dev_dependencies:
      requests_mock: '1.12.1'
```