Skip to content

Commit 26d6402

Browse files
authored
chore(weave): Add stainless codegen (#3859)
1 parent 3416b7a commit 26d6402

File tree

9 files changed

+5884
-4
lines changed

9 files changed

+5884
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ gha-creds-*.json
1919
*.log
2020
*/file::memory:?cache=shared
2121
tests/weave_models/
22+
tools/codegen/generate_config.yaml
2223
.cursor

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ prepare-release: docs build
2222
synchronize-base-object-schemas:
2323
cd weave && make generate_base_object_schemas && \
2424
cd ../weave-js && yarn generate-schemas
25+
26+
generate-bindings:
27+
python tools/codegen/generate.py $(ARGS)

pyproject.toml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ dependencies = [
4646
"gql[aiohttp,requests]", # Used exclusively in wandb_api.py
4747
"jsonschema>=4.23.0", # Used by scorers for field validation
4848
"diskcache==5.6.3", # Used for data caching
49+
50+
# This dependency will be updated each time we regenerate the trace server bindings.
51+
# 1. For normal dev, pin to a SHA and allow direct references. This will look like:
52+
# weave_server_sdk @ git+https://github.com/wandb/weave-stainless@daf91fdd07535c570eb618b343e2d125f9b09e25
53+
# 2. For deploys, pin to a specific version and remove allow-direct-references. This will look like:
54+
# weave_server_sdk==0.0.1
55+
56+
# TODO: Uncomment when ready to commit to the new bindings.
57+
"weave_server_sdk @ git+https://github.com/wandb/weave-stainless@9f62f9b3422d2afa7ad56f853ff510a81c1abb73",
4958
]
5059

5160
[project.optional-dependencies]
@@ -79,7 +88,7 @@ huggingface = ["huggingface-hub>=0.28.1"]
7988
instructor = [
8089
"instructor>=1.4.3,<1.7.0; python_version <= '3.9'",
8190
"instructor>=1.4.3; python_version > '3.9'",
82-
"google-genai>=1.5.0"
91+
"google-genai>=1.5.0",
8392
]
8493
langchain = [
8594
"langchain-core>=0.2.1",
@@ -94,7 +103,8 @@ langchain_nvidia_ai_endpoints = [
94103
"langchain-nvidia-ai-endpoints",
95104
]
96105
litellm = ["litellm>=1.36.1"]
97-
llamaindex = ["llama-index>=0.10.35,<0.12.0"] # temporary max pin b/c 0.12.0 changes call structure and therefor tests
106+
# temporary max pin b/c 0.12.0 changes call structure and therefor tests
107+
llamaindex = ["llama-index>=0.10.35,<0.12.0"]
98108
mistral0 = ["mistralai>=0.1.8,<1.0.0"]
99109
mistral1 = ["mistralai>=1.0.0"]
100110
scorers = [
@@ -105,7 +115,7 @@ scorers = [
105115
"transformers>=4.48.2",
106116
"torch>=2.4.1",
107117
"sentencepiece>=0.2.0",
108-
"pip>=20.0", # this is needed for presidio-analyzer to pull the spacy models
118+
"pip>=20.0", # this is needed for presidio-analyzer to pull the spacy models
109119
"presidio-analyzer>=2.2.0",
110120
"presidio-anonymizer>=2.2.0",
111121
]
@@ -139,10 +149,16 @@ test = [
139149
"pillow",
140150
"filelock",
141151
"httpx",
142-
152+
143153
"weave[trace_server]",
144154
"weave[trace_server_tests]",
145155
]
156+
dev = [
157+
# codegen
158+
"tomlkit",
159+
"python-multipart",
160+
161+
]
146162

147163
[project.urls]
148164
Company = "https://wandb.com"
@@ -171,6 +187,8 @@ exclude = [
171187
"weave/clear_cache.py",
172188
]
173189

190+
[tool.hatch.metadata]
191+
allow-direct-references = true
174192
[tool.pytest.ini_options]
175193
filterwarnings = [
176194
# treat warnings as errors

tools/codegen/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Weave SDK Code Generator
2+
3+
This tool generates code from the OpenAPI specification for Python, Node.js, and TypeScript using Stainless.
4+
5+
## Setup
6+
7+
1. Install weave locally:
8+
9+
```
10+
uv pip install -e .
11+
```
12+
13+
2. Set the required environment variables:
14+
15+
- `STAINLESS_API_KEY`
16+
- `GITHUB_TOKEN`
17+
18+
3. Configure your local paths:
19+
20+
```bash
21+
# Copy the template configuration file
22+
cp tools/codegen/generate_config.yaml.template tools/codegen/generate_config.yaml
23+
24+
# Edit generate_config.yaml with your local repository paths
25+
```
26+
27+
Note: Your local `generate_config.yaml` will be ignored by git to prevent checking in personal paths.
28+
29+
## Commands / ARGS
30+
31+
Note: If you run `make generate-bindings` from the weave root directory as suggested below, you'll need to pass the commands as with `ARGS=`, for example `ARGS="all"`. See more below.
32+
33+
- **get-openapi-spec**: Starts a temporary server to fetch and save the OpenAPI spec.
34+
- **generate-code**: Generates client code using Stainless. You can specify the path for code generation for each language.
35+
- **update-pyproject**: Updates pyproject.toml with either the generated git SHA (normal dev) or the published pypi version (release).
36+
- **all**: Runs the full pipeline. Configuration can be provided via a YAML file (default: tools/codegen/generate_config.yaml).
37+
38+
## Usage
39+
40+
For help, run (from the weave root directory):
41+
42+
```
43+
make generate-bindings ARGS="--help"
44+
```
45+
46+
To run the full pipeline:
47+
48+
```bash
49+
make generate-bindings ARGS="all"
50+
```
51+
52+
## Stainless configuration
53+
54+
In general, you won't need to change the Stainless configuration located at `tools/codegen/openapi.stainless.yml`. This file configures how Stainless will generate code, including options for different languages, security settings, etc.
55+
56+
In the unlikely event that you need to change the configuration, it will probably be to add or remove an endpoint from generation. To do this, navigate to the `resources` key of the config. There, you'll see a list of resources we have like `calls`, `objects`, etc. If you want to manually add or remove a method, you can directly edit the relevant section. For example, you can comment out `calls/methods/start` to remove the `call_start` method from generated clients. You can also add new sections or endpoints entirely.
57+
58+
## Adding new endpoints (TODO)
59+
60+
## Troubleshooting
61+
62+
- Ensure port 6345 is free if the server doesn't start.
63+
- Verify that all required environment variables are set.

0 commit comments

Comments
 (0)