|
1 | 1 | --- |
2 | 2 | title: Adding custom code |
3 | | -description: Augment your TypeScript SDK with custom utilities |
| 3 | +description: Augment your Python SDK with custom utilities |
4 | 4 | --- |
5 | 5 |
|
6 | | -Learn how to extend your Fern Python SDK with custom code and utilities. |
| 6 | +Fern-generated SDKs are designed to be extended with custom code. Your custom |
| 7 | +code can add additional functionality to the SDK and live in harmony with the |
| 8 | +generated code. This page explains how to configure custom logic using a |
| 9 | +`.fernignore` file, create custom SDK methods, and add additional dependencies to your Python SDK. |
7 | 10 |
|
8 | | -<Warning>This page is a WIP, please refer to our previous [documentation](https://buildwithfern.com/learn/sdks/capabilities/custom-code).</Warning> |
| 11 | +## Adding custom logic |
| 12 | + |
| 13 | +If you want your SDK to do more than just make basic API calls (like combining |
| 14 | +multiple calls, processing data, adding utilities), you can use `.fernignore` to |
| 15 | +protect your custom code from being overwritten during regeneration. |
| 16 | + |
| 17 | +Simply add your custom files to the SDK repository and list them out in `.fernignore`. Fern |
| 18 | +won't override any files that you add in `.fernignore`. |
| 19 | + |
| 20 | +To get started adding custom code: |
| 21 | + |
| 22 | +<Steps> |
| 23 | + |
| 24 | + ### Create a new file and add your custom logic |
| 25 | + |
| 26 | + |
| 27 | + ```python title="src/<package>/helper.py" |
| 28 | + def my_helper() -> None: |
| 29 | + print "Hello World!" |
| 30 | + ``` |
| 31 | + |
| 32 | + ### Add your file to `.fernignore` |
| 33 | + |
| 34 | + <Tip>A `.fernignore` file is automatically created in your SDK repository when you use GitHub publishing.</Tip> |
| 35 | + |
| 36 | + |
| 37 | + ```yaml {3} title=".fernignore" |
| 38 | + # Specify files that shouldn't be modified by Fern |
| 39 | + |
| 40 | + src/<package>/helper.py |
| 41 | + ``` |
| 42 | + |
| 43 | + ### Consume the helper |
| 44 | + |
| 45 | + Now your users can consume the helper function by importing it from the SDK: |
| 46 | + |
| 47 | + ```python |
| 48 | + from package.helper import my_helper |
| 49 | + |
| 50 | + my_helper() |
| 51 | + ``` |
| 52 | +</Steps> |
| 53 | + |
| 54 | + ## Adding custom SDK methods |
| 55 | + |
| 56 | + Fern also allows you to add custom methods to the SDK itself (e.g. |
| 57 | + `client.my_method()` ) by inheriting the Fern generated client and then |
| 58 | + extending it. |
| 59 | + |
| 60 | + <Note> |
| 61 | + See an example from ElevenLabs using this process in their [Python SDK](https://github.com/elevenlabs/elevenlabs-python/blob/main/src/elevenlabs/client.py). |
| 62 | + </Note> |
| 63 | + |
| 64 | + <Steps> |
| 65 | + ### Update `generators.yml` configuration |
| 66 | + |
| 67 | + To add a custom method to the Python SDK, you will need to configure the |
| 68 | + generator to output the client in a file called `base_client.py`. Then, you can |
| 69 | + extend the base client and add whatever methods you want. |
| 70 | + |
| 71 | + ```yaml {4-8} title="generators.yml" |
| 72 | + - name: fernapi/fern-python-sdk |
| 73 | + version: "..." |
| 74 | + config: |
| 75 | + client: |
| 76 | + class_name: BaseClient # The name of the generated client you will extend |
| 77 | + filename: base_client.py # The name of the file the generated client will live in |
| 78 | + exported_class_name: YourClient # The name of the class you will be creating that extends the generated client |
| 79 | + exported_filename: client.py |
| 80 | + ``` |
| 81 | + ### Generate the SDK |
| 82 | +
|
| 83 | + Trigger SDK generation by running `fern generate`: |
| 84 | + |
| 85 | + ```bash |
| 86 | + fern generate --group sdk |
| 87 | + ``` |
| 88 | + |
| 89 | + ### Import and extend the generated client |
| 90 | + |
| 91 | + 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. |
| 92 | + |
| 93 | + ```python title="src/<package>/client.py" |
| 94 | + from .base_client import \ |
| 95 | + BaseClient |
| 96 | +
|
| 97 | + class YourClient(BaseClient): |
| 98 | +
|
| 99 | + def my_helper(self) -> None |
| 100 | + print("Hello World") |
| 101 | +
|
| 102 | + ``` |
| 103 | + |
| 104 | + <Note> |
| 105 | + See an example [client.py](https://github.com/elevenlabs/elevenlabs-python/blob/main/src/elevenlabs/client.py) from ElevenLabs. |
| 106 | + </Note> |
| 107 | + |
| 108 | + |
| 109 | + ### Update `.fernignore` |
| 110 | + |
| 111 | + Add the `client.py` to `.fernignore`. |
| 112 | + |
| 113 | + ```diff title=".fernignore" |
| 114 | + + src/<package>/client.py |
| 115 | + ``` |
| 116 | + |
| 117 | + <Note> |
| 118 | + See an example [.fernignore](https://github.com/elevenlabs/elevenlabs-python/blob/main/.fernignore) from ElevenLabs. |
| 119 | + </Note> |
| 120 | + |
| 121 | + |
| 122 | + ### Consume the method |
| 123 | + |
| 124 | + Now your users can consume the helper function by importing it from the SDK: |
| 125 | + |
| 126 | + ```python |
| 127 | + client.my_helper() |
| 128 | + ``` |
| 129 | + </Steps> |
| 130 | + |
| 131 | + |
| 132 | +## Adding custom dependencies |
| 133 | + |
| 134 | +To add packages that your custom code requires, update your `generators.yml`. |
| 135 | + |
| 136 | + ```yaml {4-7} title="generators.yml" |
| 137 | + - name: fernapi/fern-python-sdk |
| 138 | + version: "..." |
| 139 | + config: |
| 140 | + extra_dependencies: |
| 141 | + numpy: '1.2.0' |
| 142 | + extra_dev_dependencies: |
| 143 | + requests_mock: '1.12.1' |
| 144 | + ``` |
0 commit comments