Skip to content

Commit eff9550

Browse files
dbrattliclaude
andcommitted
docs: add README for pydantic example and update fastapi README
- Add README.md for pydantic example explaining how to import Python models - Update fastapi README to clarify it shows the opposite pattern (F# to Python) - Fix duplicate "API Endpoints" section heading in fastapi README - Cross-reference between the two examples 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent c2dc0fc commit eff9550

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

examples/fastapi/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# FastAPI Example
1+
# FastAPI Example (Generating Models from F#)
22

3-
This example demonstrates how to build a FastAPI application using **Fable.Python** with the decorator-based API pattern.
3+
This example demonstrates how to build a FastAPI application using **Fable.Python** with the decorator-based API pattern. The Pydantic models are **defined in F#** and compile to Python.
4+
5+
> **Note:** For the opposite approach (importing Pydantic models defined in Python), see the [Pydantic example](../pydantic/).
46
57
## Features
68

@@ -95,7 +97,7 @@ type User(Id: int, Name: string, Email: string) =
9597
member val Email: string = Email with get, set
9698
```
9799

98-
### API Endpoints
100+
### Defining Endpoints
99101

100102
The `[<APIClass>]` attribute marks a class for FastAPI routing, and method decorators define the HTTP methods:
101103

examples/pydantic/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Pydantic Example (Importing Python Models)
2+
3+
This example demonstrates how to **import and use Pydantic models defined in Python** from F# code using Fable.Python.
4+
5+
> **Note:** This is the opposite approach from the [FastAPI example](../fastapi/), which shows how to **define Pydantic models in F#** that compile to Python.
6+
7+
## Use Cases
8+
9+
This pattern is useful when you want to:
10+
11+
- Use models generated from OpenAPI specs or other tools
12+
- Use models maintained by a Python team
13+
- Integrate with an existing Python codebase
14+
- Share models between Python and F# code
15+
16+
## Project Structure
17+
18+
```txt
19+
examples/pydantic/
20+
├── App.fs # F# application using the models
21+
├── Models.fs # F# bindings for Python models
22+
├── models.py # Handwritten Python Pydantic models
23+
├── PydanticExample.fsproj
24+
├── build/ # Generated Python code (git-ignored)
25+
└── README.md
26+
```
27+
28+
## How It Works
29+
30+
### 1. Define Pydantic Models in Python
31+
32+
Create your models in `models.py`:
33+
34+
```python
35+
from pydantic import BaseModel
36+
37+
class User(BaseModel):
38+
id: int
39+
name: str
40+
email: str | None = None
41+
age: int | None = None
42+
```
43+
44+
### 2. Create F# Bindings
45+
46+
Import the Python models using `[<Import>]`:
47+
48+
```fsharp
49+
[<Import("User", "models")>]
50+
type User =
51+
abstract id: int with get, set
52+
abstract name: string with get, set
53+
abstract email: string option with get, set
54+
abstract age: int option with get, set
55+
```
56+
57+
### 3. Create Constructors
58+
59+
Use `[<Emit>]` to call the Python constructor with named arguments:
60+
61+
```fsharp
62+
[<RequireQualifiedAccess>]
63+
module User =
64+
[<Import("User", "models")>]
65+
[<Emit("$0(id=$1, name=$2, email=$3, age=$4)")>]
66+
let create (id: int) (name: string) (email: string option) (age: int option) : User = nativeOnly
67+
```
68+
69+
### 4. Use in F# Code
70+
71+
```fsharp
72+
let user = User.create 1 "Alice" (Some "[email protected]") (Some 30)
73+
printfn "User: %s (id=%d)" user.name user.id
74+
```
75+
76+
## Building
77+
78+
From the repository root:
79+
80+
```bash
81+
# Using justfile
82+
just example-pydantic
83+
84+
# Or manually
85+
cd examples/pydantic
86+
dotnet fable --lang python --outDir build
87+
cp models.py build/
88+
cd build && python app.py
89+
```
90+
91+
## Output
92+
93+
```txt
94+
User 1: Alice (id=1)
95+
User 1 email: [email protected]
96+
User 1 age: 30
97+
98+
User 2: Bob (id=2)
99+
User 2 email: None
100+
101+
Product: Laptop - $1299.99
102+
In stock: true
103+
Tags: ['electronics', 'computers']
104+
105+
Updated email: [email protected]
106+
107+
Create request for: Charlie
108+
```
109+
110+
## See Also
111+
112+
- [FastAPI example](../fastapi/) - Shows the opposite pattern: defining Pydantic models in F# that compile to Python

0 commit comments

Comments
 (0)