Adding custom code

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

Adding custom logic

To get started adding custom code:

1

Create a new file and add your custom logic

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

Add your file to .fernignore

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

Consume the helper

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

1from package.helper import my_helper
2
3my_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
1- name: fernapi/fern-python-sdk
2 version: "..."
3 config:
4 client:
5 class_name: BaseClient # The name of the generated client you will extend
6 filename: base_client.py # The name of the file the generated client will live in
7 exported_class_name: YourClient # The name of the class you will be creating that extends the generated client
8 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
1 from .base_client import BaseClient // import generated client
2
3 class YourClient(BaseClient): // extend generated client
4 def my_helper(self) -> None:
5 print("Hello World!")
6 def my_helper(self) -> None:
7 print("Hello World")

See an example client.py from ElevenLabs.

4

Update .fernignore

Add the client.py to .fernignore.

.fernignore
1+ 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.

1client.my_helper()

Adding custom dependencies

Pro Feature

This feature is only available on paid plans. Please schedule a demo or email us to get started.

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

generators.yml
1- name: fernapi/fern-python-sdk
2 version: "..."
3 config:
4 extra_dependencies:
5 numpy: '1.2.0'
6 extra_dev_dependencies:
7 requests_mock: '1.12.1'