Skip to navigation

Adding custom code

View as Markdown
Enterprise feature

This feature is available only for the Enterprise plan. To get started, reach out to 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:

These steps cover adding a new custom file to the SDK. To preserve line-level edits to a generated file, use Replay instead.
1

Create a new file and add your custom logic

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

Add your file to .fernignore

.fernignore
# Specify files that shouldn't be modified by Fern
src/<package>/helper.py
3

Consume the helper

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

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.

See an example from ElevenLabs using this process in their Python SDK.

1

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
- 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
2

Generate the SDK

Trigger SDK generation by running fern generate:

fern generate --group sdk
3

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
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")

See an example client.py from ElevenLabs.

4

Update .fernignore

Add the client.py to .fernignore.

.fernignore
+ src/<package>/client.py

See an example .fernignore from ElevenLabs.

5

Consume the method

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

client.my_helper()

Adding custom dependencies

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

generators.yml
- name: fern-python-sdk
version: "..."
config:
extra_dependencies:
numpy: '1.2.0'
extra_dev_dependencies:
requests_mock: '1.12.1'