Python Configuration

You can customize the behavior of the Python SDK generator in generators.yml:

generators.yml
1default-group: local
2groups:
3 local:
4 generators:
5 - name: fernapi/fern-python
6 version: 4.26.2
7 config:
8 package_name: "your_package"
9 client:
10 class_name: "YourClient"
11 additional_init_exports:
12 - from: file_with_custom_function
13 imports:
14 - custom_function
15 pydantic_config:
16 skip_validation: true

SDK Configuration Options

additional_init_exports
array of objectsDefaults to null

Additional modules or classes to export from the package’s __init__.py file. This allows you to customize what’s available when users import your package.

Each object should specify which file to import from and what to import:

1config:
2 additional_init_exports:
3 - from: core.oauth_flow
4 imports:
5 - validate_token
6 - from: utils.helpers
7 imports:
8 - format_currency
9 - PhoneValidator

This enables users to access your custom functions directly:

1from my_package import validate_token, format_currency, PhoneValidator
additional_init_exports[].from
stringRequired

The module path to import from, using Python dot notation. Omit the .py extension and replace path separators with dots.

For example, if you want to import from the file core/oauth_flow.py, specify that as - from: core.oauth_flow.

additional_init_exports[].imports
array of stringsRequired

List of class names, function names, or other objects to import from the specified file.

default_bytes_stream_chunk_size
numberDefaults to null

The chunk size to use (if any) when processing a response bytes stream within iter_bytes or aiter_bytes results in: for chunk in response.iter_bytes(chunk_size=<default_bytes_stream_chunk_size>):

exclude_types_from_init_exports
boolDefaults to false

When enabled, excludes type definitions from being exported in the package’s __init__.py file, reducing the public API surface.

extra_dependencies
objectDefaults to {}

If you want to add custom dependencies to your generated SDK, you can specify them using this configuration. For example, to add a dependency on boto3, your config would look like:

1config:
2 extra_dependencies:
3 boto3: 1.28.15
extra_dev_dependencies
objectDefaults to {}

Specify additional development dependencies to include in the generated SDK’s setup configuration. These are dependencies used for development and testing but not required by end users.

extras
objectDefaults to {}

Define optional dependency groups that users can install with your package using pip extras (e.g., pip install your-package[extra-name]).

flat_layout
boolDefaults to false

When enabled, generates a flatter package structure by reducing nested directories and modules.

follow_redirects_by_default
boolDefaults to true

Whether to follow redirects by default in HTTP requests.

improved_imports
boolDefaults to true

Feature flag that improves imports in the Python SDK by removing nested resources directory

include_legacy_wire_tests
boolDefaults to false

Whether or not to include legacy wire tests in the generated SDK

include_union_utils
boolDefaults to false

When enabled, generates utility methods for working with union types, including factory methods and visitor patterns.

inline_path_params
boolDefaults to false

If true, treats path parameters as named parameters in endpoint functions.

inline_request_params
boolDefaults to true

Feature flag that removes the usage of request objects, and instead uses parameters in function signatures where possible.

package_name
stringDefaults to null

Specifies the Python package name that users will import your generated client from.

For example, setting package_name: "my_custom_package" enables users to use my_custom_package import Client to import your client:

1default-group: local
2groups:
3 local:
4 generators:
5 - name: fernapi/fern-python
6 version: 4.26.2
7 config:
8 package_name: "my_custom_package"
pyproject_python_version
stringDefaults to ^3.8
This changes your declared python dependency, which is not meant to be done often if at all. This is a last resort if any dependencies force you to change your version requirements.
pyproject_toml
stringDefaults to null

Path to a custom pyproject.toml template file to use instead of the default generated one.

should_generate_websocket_clients
boolDefaults to false

Feature flag that enables generation of Python websocket clients.

skip_formatting
boolDefaults to false

When enabled, skips code formatting (like black) on the generated Python code.

timeout_in_seconds
number | 'infinity'Defaults to 60

By default, the generator generates a client that times out after 60 seconds. You can customize this value by providing a different number or setting to infinity to get rid of timeouts.

use_api_name_in_package
boolDefaults to false

When enabled, includes the API name as part of the package structure and naming.

use_inheritance_for_extended_models
boolDefaults to true

Whether to generate Pydantic models that implement inheritance when a model utilizes the Fern extends keyword.

use_typeddict_requests
boolDefaults to false

Whether or not to generate TypedDicts instead of Pydantic Models for request objects.

use_typeddict_requests_for_file_upload
boolDefaults to false

Whether or not to generate TypedDicts instead of Pydantic Models for file upload request objects. Note that this flag was only introduced due to an oversight in the use_typeddict_requests flag implementation; it should be removed in the future.

client

Configuration for the generated client class and file structure.

1config:
2 client:
3 filename: "my_client.py"
4 class_name: "MyClient"
5 exported_filename: "my_client.py"
6 exported_class_name: "MyClient"
filename
stringDefaults to client.py

The filename for the generated client file.

class_name
stringDefaults to null

The name of the generated client class.

exported_filename
stringDefaults to client.py

The filename of the exported client which will be used in code snippets.

exported_class_name
stringDefaults to null

The name of the exported client class that will be used in code snippets.

pydantic_config

Configure Pydantic model generation settings for your Python SDK.

1config:
2 pydantic_config:
3 enum_type: "literals"
4 extra_fields: "forbid"
5 frozen: true
6 include_union_utils: false
7 include_validators: true
8 orm_mode: false
9 require_optional_fields: false
10 skip_formatting: false
11 skip_validation: true
12 smart_union: true
13 union_naming: "v0"
14 use_inheritance_for_extended_models: true
15 use_pydantic_field_aliases: false
16 use_provided_defaults: true
17 use_str_enums: true
18 use_typeddict_requests: false
19 wrapped_aliases: false
enum_type
'literals' | 'forward_compatible_python_enums' | 'python_enums'Defaults to literals

The type of enums to use in the generated models:

  • literals: Use Python Literal types, e.g. MyEnum = Literal["foo", "bar"]
  • forward_compatible_python_enums: Use Python Enum classes, with a MyEnum._UNKNOWN member for forward compatibility. MyEnum._UNKNOWN.value contains the raw unrecognized value.
  • python_enums: Your vanilla Python enum class, with the members defined within your API.
extra_fields
literal<'allow' | 'forbid' | 'ignore'>Defaults to allow

How to handle extra fields not defined in the model schema.

frozen
boolDefaults to true

Whether Pydantic models should be frozen (immutable after creation).

include_union_utils
boolDefaults to false

When enabled, the generator will output a Pydantic __root__ class that will contain utilities to visit the union. For example, for the following union type:

types:
Shape:
union:
circle: Circle
triangle: Triangle

you will get a generated Shape class that has a factory and visitor:

1# Use a factory to instantiate the union
2Shape.factory.circle(Circle(...))
3
4# Visit every case in the union
5shape = get_shape()
6shape.visit(
7 circle: lambda circle: do_something_with_circle(circle),
8 triangle: lambda triangle: do_something_with_triangle(triangle),
9)
include_validators
boolDefaults to false

Include custom validators in generated Pydantic models.

orm_mode
boolDefaults to false

Enable ORM mode for Pydantic models to work with ORMs like SQLAlchemy.

package_name
string

Custom package name for the generated models.

require_optional_fields
boolDefaults to false

Whether optional fields must be explicitly provided (cannot be omitted).

skip_formatting
boolDefaults to false

Skip code formatting for generated Pydantic models.

skip_validation
boolDefaults to false

When enabled, disables Pydantic validation for API responses. This ensures that Pydantic does not immediately fail if the model being returned from an API does not exactly match the Pydantic model.

This is meant to add flexibility should your SDK fall behind your API, but should be used sparingly, as the type-hinting for users will still reflect the Pydantic model exactly.

smart_union
boolDefaults to true

Enable smart union handling in Pydantic models for better type discrimination.

union_naming
'v0' | 'v1'Defaults to v0

Control union naming strategy. If you are dealing with discriminated union members that already have the discriminant property on them (and they’re only used in one union), you should prefer the global API config within your generators.yml:

1- name: fernapi/fern-python-sdk
2 version: 3.0.0-rc0
3 api:
4 settings:
5 unions: v1
use_pydantic_field_aliases
boolDefaults to false

Use Pydantic field aliases for property names that differ from wire format.

use_provided_defaults
boolDefaults to false

Leverage defaults specified in the API specification.

use_typeddict_requests
boolDefaults to false

Generate TypedDicts instead of Pydantic Models for request objects.

version
'v1' | 'v2' | 'both' | 'v1_on_v2'Defaults to both

By default, the generator generates pydantic models that are v1 and v2 compatible. However you can override them to:

  • v1: strictly use Pydantic v1
  • v2: strictly use Pydantic v2
  • both: maintain compatibility with both versions
  • v1_on_v2: use Pydantic v1 compatibility layer on v2
wrapped_aliases
boolDefaults to false

Enable wrapped aliases for Pydantic models. Only supported in Pydantic V1, V1_ON_V2, or V2.

use_inheritance_for_extended_models
boolDefaults to true

Generate Pydantic models that implement inheritance when a model utilizes the Fern extends keyword.