|
| 1 | +# Letta Python Library |
| 2 | + |
| 3 | +[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Ffern-demo%2Fletta-python-sdk) |
| 4 | +[](https://pypi.python.org/pypi/letta) |
| 5 | + |
| 6 | +The Letta Python library provides convenient access to the Letta API from Python. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```sh |
| 11 | +pip install letta |
| 12 | +``` |
| 13 | + |
| 14 | +## Reference |
| 15 | + |
| 16 | +A full reference for this library is available [here](./reference.md). |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Instantiate and use the client with the following: |
| 21 | + |
| 22 | +```python |
| 23 | +from letta import Letta |
| 24 | + |
| 25 | +client = Letta( |
| 26 | + token="YOUR_TOKEN", |
| 27 | +) |
| 28 | +client.tools.create( |
| 29 | + source_code="source_code", |
| 30 | +) |
| 31 | +``` |
| 32 | + |
| 33 | +## Async Client |
| 34 | + |
| 35 | +The SDK also exports an `async` client so that you can make non-blocking calls to our API. |
| 36 | + |
| 37 | +```python |
| 38 | +import asyncio |
| 39 | + |
| 40 | +from letta import AsyncLetta |
| 41 | + |
| 42 | +client = AsyncLetta( |
| 43 | + token="YOUR_TOKEN", |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +async def main() -> None: |
| 48 | + await client.tools.create( |
| 49 | + source_code="source_code", |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +asyncio.run(main()) |
| 54 | +``` |
| 55 | + |
| 56 | +## Exception Handling |
| 57 | + |
| 58 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 59 | +will be thrown. |
| 60 | + |
| 61 | +```python |
| 62 | +from letta.core.api_error import ApiError |
| 63 | + |
| 64 | +try: |
| 65 | + client.tools.create(...) |
| 66 | +except ApiError as e: |
| 67 | + print(e.status_code) |
| 68 | + print(e.body) |
| 69 | +``` |
| 70 | + |
| 71 | +## Advanced |
| 72 | + |
| 73 | +### Retries |
| 74 | + |
| 75 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 76 | +as the request is deemed retriable and the number of retry attempts has not grown larger than the configured |
| 77 | +retry limit (default: 2). |
| 78 | + |
| 79 | +A request is deemed retriable when any of the following HTTP status codes is returned: |
| 80 | + |
| 81 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 82 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 83 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 84 | + |
| 85 | +Use the `max_retries` request option to configure this behavior. |
| 86 | + |
| 87 | +```python |
| 88 | +client.tools.create(..., request_options={ |
| 89 | + "max_retries": 1 |
| 90 | + }) |
| 91 | + ``` |
| 92 | + |
| 93 | +### Timeouts |
| 94 | + |
| 95 | +The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level. |
| 96 | + |
| 97 | +```python |
| 98 | + |
| 99 | + from letta import Letta |
| 100 | + |
| 101 | +client = Letta( |
| 102 | + ..., |
| 103 | + timeout=20.0, |
| 104 | +) |
| 105 | + |
| 106 | + |
| 107 | + # Override timeout for a specific method |
| 108 | + client.tools.create(..., request_options={ |
| 109 | + "timeout_in_seconds": 1 |
| 110 | + }) |
| 111 | + ``` |
| 112 | + |
| 113 | +### Custom Client |
| 114 | + |
| 115 | +You can override the `httpx` client to customize it for your use-case. Some common use-cases include support for proxies |
| 116 | +and transports. |
| 117 | +```python |
| 118 | +import httpx |
| 119 | +from letta import Letta |
| 120 | + |
| 121 | +client = Letta( |
| 122 | + ..., |
| 123 | + httpx_client=httpx.Client( |
| 124 | + proxies="http://my.test.proxy.example.com", |
| 125 | + transport=httpx.HTTPTransport(local_address="0.0.0.0"), |
| 126 | + ), |
| 127 | +) |
| 128 | +``` |
| 129 | + |
| 130 | +## Contributing |
| 131 | + |
| 132 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 133 | +Additions made directly to this library would have to be moved over to our generation code, |
| 134 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 135 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 136 | +an issue first to discuss with us! |
| 137 | + |
| 138 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments