| 1 |
## Contributing to documentation |
| 2 |
|
| 3 |
The documentation for this SDK lives at [platform.claude.com/docs/en/api/sdks/python](https://platform.claude.com/docs/en/api/sdks/python). To suggest changes, open an issue. |
| 4 |
|
| 5 |
## Setting up the environment |
| 6 |
|
| 7 |
### With `uv` |
| 8 |
|
| 9 |
We use [uv](https://docs.astral.sh/uv/) to manage dependencies because it will automatically provision a Python environment with the expected Python version. To set it up, run: |
| 10 |
|
| 11 |
```sh |
| 12 |
$ ./scripts/bootstrap |
| 13 |
``` |
| 14 |
|
| 15 |
Or [install uv manually](https://docs.astral.sh/uv/getting-started/installation/) and run: |
| 16 |
|
| 17 |
```sh |
| 18 |
$ uv sync --all-extras |
| 19 |
``` |
| 20 |
|
| 21 |
You can then run scripts using `uv run python script.py` or by manually activating the virtual environment: |
| 22 |
|
| 23 |
```sh |
| 24 |
# manually activate - https://docs.python.org/3/library/venv.html#how-venvs-work |
| 25 |
$ source .venv/bin/activate |
| 26 |
|
| 27 |
# now you can omit the `uv run` prefix |
| 28 |
$ python script.py |
| 29 |
``` |
| 30 |
|
| 31 |
### Without `uv` |
| 32 |
|
| 33 |
Alternatively if you don't want to install `uv`, you can stick with the standard `pip` setup by ensuring you have the Python version specified in `.python-version`, create a virtual environment however you desire and then install dependencies using this command: |
| 34 |
|
| 35 |
```sh |
| 36 |
$ pip install -r requirements-dev.lock |
| 37 |
``` |
| 38 |
|
| 39 |
## Modifying/Adding code |
| 40 |
|
| 41 |
Most of the SDK is generated code. Modifications to code will be persisted between generations, but may |
| 42 |
result in merge conflicts between manual patches and changes from the generator. The generator will never |
| 43 |
modify the contents of the `src/anthropic/lib/` and `examples/` directories. |
| 44 |
|
| 45 |
## Adding and running examples |
| 46 |
|
| 47 |
All files in the `examples/` directory are not modified by the generator and can be freely edited or added to. |
| 48 |
|
| 49 |
```py |
| 50 |
# add an example to examples/<your-example>.py |
| 51 |
|
| 52 |
#!/usr/bin/env -S uv run python |
| 53 |
… |
| 54 |
``` |
| 55 |
|
| 56 |
```sh |
| 57 |
$ chmod +x examples/<your-example>.py |
| 58 |
# run the example against your api |
| 59 |
$ ./examples/<your-example>.py |
| 60 |
``` |
| 61 |
|
| 62 |
## Using the repository from source |
| 63 |
|
| 64 |
If you’d like to use the repository from source, you can either install from git or link to a cloned repository: |
| 65 |
|
| 66 |
To install via git: |
| 67 |
|
| 68 |
```sh |
| 69 |
$ pip install git+ssh://[email protected]/anthropics/anthropic-sdk-python.git |
| 70 |
``` |
| 71 |
|
| 72 |
Alternatively, you can build from source and install the wheel file: |
| 73 |
|
| 74 |
Building this package will create two files in the `dist/` directory, a `.tar.gz` containing the source files and a `.whl` that can be used to install the package efficiently. |
| 75 |
|
| 76 |
To create a distributable version of the library, all you have to do is run this command: |
| 77 |
|
| 78 |
```sh |
| 79 |
$ uv build |
| 80 |
# or |
| 81 |
$ python -m build |
| 82 |
``` |
| 83 |
|
| 84 |
Then to install: |
| 85 |
|
| 86 |
```sh |
| 87 |
$ pip install ./path-to-wheel-file.whl |
| 88 |
``` |
| 89 |
|
| 90 |
## Running tests |
| 91 |
|
| 92 |
Most tests require you to [set up a mock server](https://github.com/dgellow/steady) against the OpenAPI spec to run the tests. |
| 93 |
|
| 94 |
```sh |
| 95 |
$ ./scripts/mock |
| 96 |
``` |
| 97 |
|
| 98 |
```sh |
| 99 |
$ ./scripts/test |
| 100 |
``` |
| 101 |
|
| 102 |
### Snapshots |
| 103 |
|
| 104 |
Some tests use [inline-snapshot](https://15r10nk.github.io/inline-snapshot/latest/). To update them after making changes, rerun the tests with the `--inline-snapshot=fix` and `-n0` options: |
| 105 |
```bash |
| 106 |
./scripts/test --inline-snapshot=fix -n0 |
| 107 |
``` |
| 108 |
|
| 109 |
> [!NOTE] |
| 110 |
> `inline-snapshot` is incompatible with [pytest-xdist](https://github.com/pytest-dev/pytest-xdist), so you need to disable parallel execution `(-n0)` when using the `--inline-snapshot` option. |
| 111 |
|
| 112 |
In addition, some tests capture snapshots of the HTTP requests they make. |
| 113 |
To refresh these snapshots, run the tests with the `--http-record` flag: |
| 114 |
```bash |
| 115 |
./scripts/test --inline-snapshot=fix --http-record -n0 |
| 116 |
``` |
| 117 |
|
| 118 |
> [!NOTE] |
| 119 |
> Sometimes it makes sense to update only the inline snapshots `(--inline-snapshot=fix)` without refreshing the HTTP snapshots `(--http-record)`. |
| 120 |
> This is useful when the endpoint hasn't changed, but your code handles the response differently and the assertions need updating. |
| 121 |
|
| 122 |
## Linting and formatting |
| 123 |
|
| 124 |
This repository uses [ruff](https://github.com/astral-sh/ruff) to lint and format the code in the repository. |
| 125 |
|
| 126 |
To lint: |
| 127 |
|
| 128 |
```sh |
| 129 |
$ ./scripts/lint |
| 130 |
``` |
| 131 |
|
| 132 |
To format and fix all ruff issues automatically: |
| 133 |
|
| 134 |
```sh |
| 135 |
$ ./scripts/format |
| 136 |
``` |
| 137 |
|
| 138 |
## Publishing and releases |
| 139 |
|
| 140 |
Changes made to this repository via the automated release PR pipeline should publish to PyPI automatically. If |
| 141 |
the changes aren't made through the automated pipeline, you may want to make releases manually. |
| 142 |
|
| 143 |
### Publish with a GitHub workflow |
| 144 |
|
| 145 |
You can release to package managers by using [the `Publish PyPI` GitHub action](https://www.github.com/anthropics/anthropic-sdk-python/actions/workflows/publish-pypi.yml). This requires a setup organization or repository secret to be set up. |
| 146 |
|
| 147 |
### Publish manually |
| 148 |
|
| 149 |
If you need to manually release a package, you can run the `bin/publish-pypi` script with a `PYPI_TOKEN` set on |
| 150 |
the environment. |