Skip to content

Commit c64c517

Browse files
docs: Document how to disable Pydantic validation in Python SDKs (#344)
Co-authored-by: promptless[bot] <179508745+promptless[bot]@users.noreply.github.com> Co-authored-by: Devin Logan <[email protected]>
1 parent 9a5dabb commit c64c517

File tree

1 file changed

+140
-47
lines changed

1 file changed

+140
-47
lines changed

fern/products/sdks/overview/python/configuration.mdx

Lines changed: 140 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ description: Configuration options for the Fern Python SDK.
55

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

8-
```yaml {7-10}
8+
```yaml {7-12}
99
default-group: local
1010
groups:
1111
local:
1212
generators:
1313
- name: fernapi/fern-python
14-
version: 0.7.1
14+
version: <Markdown src="/snippets/version-number-python.mdx"/>
1515
config:
1616
package_name: "your_package"
1717
client:
1818
class_name: "YourClient"
19+
pydantic-config:
20+
skip_validation: true
1921
```
2022
2123
## SDK Configuration Options
@@ -85,56 +87,12 @@ groups:
8587
local:
8688
generators:
8789
- name: fernapi/fern-python
88-
version: 0.7.1
90+
version: <Markdown src="/snippets/version-number-python.mdx"/>
8991
config:
9092
package_name: "my_custom_package"
9193
```
9294
</ParamField>
9395

94-
<ParamField path="pydantic_config" type="SdkPydanticModelCustomConfig" default="SdkPydanticModelCustomConfig()" required={false} toc={true}>
95-
</ParamField>
96-
97-
<ParamField path="pydantic_config.include_union_utils" type="bool" default="false" required={false} toc={true}>
98-
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:
99-
100-
```
101-
types:
102-
Shape:
103-
union:
104-
circle: Circle
105-
triangle: Triangle
106-
```
107-
you will get a generated `Shape` class that has a factory and visitor:
108-
```python
109-
# Use a factory to instantiate the union
110-
Shape.factory.circle(Circle(...))
111-
112-
# Visit every case in the union
113-
shape = get_shape()
114-
shape.visit(
115-
circle: lambda circle: do_something_with_circle(circle),
116-
triangle: lambda triangle: do_something_with_triangle(triangle),
117-
)
118-
```
119-
120-
When enabled, the python generator will not run Black formatting in the generated code. Black is slow so this can potentially speed up code generation quite a bit.
121-
</ParamField>
122-
123-
<ParamField path="pydantic_config.version" type="'v1' | 'v2' | 'both' | 'v1_on_v2'" default="both" required={false} toc={true}>
124-
By default, the generator generates pydantic models that are v1 and v2 compatible. However you can override them to:
125-
- `v1`: strictly use Pydantic v1
126-
- `v2`: strictly use Pydantic v2
127-
- `both`: maintain compatibility with both versions
128-
- `v1_on_v2`: use Pydantic v1 compatibility layer on v2
129-
130-
Example:
131-
```yaml
132-
config:
133-
pydantic_config:
134-
version: v1 # or v2 or "both"
135-
```
136-
</ParamField>
137-
13896
<ParamField path="pyproject_python_version" type="string" default="^3.8" required={false} toc={true}>
13997
<Warning>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.</Warning>
14098
</ParamField>
@@ -194,4 +152,139 @@ groups:
194152

195153
<ParamField path="exported_class_name" type="string" default="null" required={false} toc={true}>
196154
The name of the exported client class that will be used in code snippets.
155+
</ParamField>
156+
157+
### pydantic-config
158+
159+
Configure Pydantic model generation settings for your Python SDK.
160+
161+
```yaml
162+
config:
163+
pydantic-config:
164+
enum_type: "literals"
165+
extra_fields: "forbid"
166+
frozen: true
167+
include_union_utils: false
168+
include_validators: true
169+
orm_mode: false
170+
require_optional_fields: false
171+
skip_formatting: false
172+
skip_validation: true
173+
smart_union: true
174+
union_naming: "v0"
175+
use_inheritance_for_extended_models: true
176+
use_pydantic_field_aliases: false
177+
use_provided_defaults: true
178+
use_str_enums: true
179+
use_typeddict_requests: false
180+
wrapped_aliases: false
181+
```
182+
183+
<ParamField path="enum_type" type="'literals' | 'forward_compatible_python_enums' | 'python_enums'" default="literals" toc={true}>
184+
The type of enums to use in the generated models:
185+
- `literals`: Use Python Literal types, e.g. `MyEnum = Literal["foo", "bar"]`
186+
- `forward_compatible_python_enums`: Use Python Enum classes, with a `MyEnum._UNKNOWN` member for forward compatibility. `MyEnum._UNKNOWN.value` contains the raw unrecognized value.
187+
- `python_enums`: Your vanilla Python enum class, with the members defined within your API.
188+
</ParamField>
189+
190+
<ParamField path="extra_fields" type="literal<'allow' | 'forbid' | 'ignore'>" default="allow" toc={true}>
191+
How to handle extra fields not defined in the model schema.
192+
</ParamField>
193+
194+
<ParamField path="frozen" type="bool" default="true" toc={true}>
195+
Whether Pydantic models should be frozen (immutable after creation).
196+
</ParamField>
197+
198+
<ParamField path="include_union_utils" type="bool" default="false" toc={true}>
199+
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:
200+
201+
```
202+
types:
203+
Shape:
204+
union:
205+
circle: Circle
206+
triangle: Triangle
207+
```
208+
you will get a generated `Shape` class that has a factory and visitor:
209+
```python
210+
# Use a factory to instantiate the union
211+
Shape.factory.circle(Circle(...))
212+
213+
# Visit every case in the union
214+
shape = get_shape()
215+
shape.visit(
216+
circle: lambda circle: do_something_with_circle(circle),
217+
triangle: lambda triangle: do_something_with_triangle(triangle),
218+
)
219+
```
220+
</ParamField>
221+
222+
<ParamField path="include_validators" type="bool" default="false" toc={true}>
223+
Include custom validators in generated Pydantic models.
224+
</ParamField>
225+
226+
<ParamField path="orm_mode" type="bool" default="false" toc={true}>
227+
Enable ORM mode for Pydantic models to work with ORMs like SQLAlchemy.
228+
</ParamField>
229+
230+
<ParamField path="package_name" type="string" required={false} toc={true}>
231+
Custom package name for the generated models.
232+
</ParamField>
233+
234+
<ParamField path="require_optional_fields" type="bool" default="false" toc={true}>
235+
Whether optional fields must be explicitly provided (cannot be omitted).
236+
</ParamField>
237+
238+
<ParamField path="skip_formatting" type="bool" default="false" toc={true}>
239+
Skip code formatting for generated Pydantic models.
240+
</ParamField>
241+
242+
<ParamField path="skip_validation" type="bool" default="false" toc={true}>
243+
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.
244+
245+
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.
246+
</ParamField>
247+
248+
<ParamField path="smart_union" type="bool" default="true" toc={true}>
249+
Enable smart union handling in Pydantic models for better type discrimination.
250+
</ParamField>
251+
252+
<ParamField path="union_naming" type="'v0' | 'v1'" default="v0" toc={true}>
253+
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`:
254+
```yaml
255+
- name: fernapi/fern-python-sdk
256+
version: 3.0.0-rc0
257+
api:
258+
settings:
259+
unions: v1
260+
```
261+
</ParamField>
262+
263+
<ParamField path="use_pydantic_field_aliases" type="bool" default="false" toc={true}>
264+
Use Pydantic field aliases for property names that differ from wire format.
265+
</ParamField>
266+
267+
<ParamField path="use_provided_defaults" type="bool" default="false" toc={true}>
268+
Leverage defaults specified in the API specification.
269+
</ParamField>
270+
271+
<ParamField path="use_typeddict_requests" type="bool" default="false" toc={true}>
272+
Generate TypedDicts instead of Pydantic Models for request objects.
273+
</ParamField>
274+
275+
<ParamField path="version" type="'v1' | 'v2' | 'both' | 'v1_on_v2'" default="both" toc={true}>
276+
By default, the generator generates pydantic models that are v1 and v2 compatible. However you can override them to:
277+
- `v1`: strictly use Pydantic v1
278+
- `v2`: strictly use Pydantic v2
279+
- `both`: maintain compatibility with both versions
280+
- `v1_on_v2`: use Pydantic v1 compatibility layer on v2
281+
282+
</ParamField>
283+
284+
<ParamField path="wrapped_aliases" type="bool" default="false" toc={true}>
285+
Enable wrapped aliases for Pydantic models. Only supported in Pydantic V1, V1_ON_V2, or V2.
286+
</ParamField>
287+
288+
<ParamField path="use_inheritance_for_extended_models" type="bool" default="true" toc={true}>
289+
Generate Pydantic models that implement inheritance when a model utilizes the Fern `extends` keyword.
197290
</ParamField>

0 commit comments

Comments
 (0)