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