trusted-router-py 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: astral-sh/setup-uv@v3
15
+ with:
16
+ version: "latest"
17
+ - run: uv sync --group dev
18
+ - run: uv run ruff check .
19
+ - run: uv run pytest
20
+ - name: Upload coverage XML for downstream tools
21
+ if: always()
22
+ uses: actions/upload-artifact@v4
23
+ with:
24
+ name: coverage-xml
25
+ path: coverage.xml
26
+ if-no-files-found: ignore
@@ -0,0 +1,48 @@
1
+ name: Release to PyPI
2
+
3
+ # Triggered when a `vX.Y.Z` tag is pushed. Uses PyPI Trusted Publishing
4
+ # (OIDC) — no API token needed once the publisher is configured at
5
+ # https://pypi.org/manage/account/publishing/. The publisher must match:
6
+ # PyPI project name : trusted-router-py
7
+ # Owner : Lore-Hex
8
+ # Repository : trusted-router-py
9
+ # Workflow filename : release.yml
10
+ # Environment : pypi (matches the env: line below)
11
+ on:
12
+ push:
13
+ tags:
14
+ - "v*"
15
+
16
+ jobs:
17
+ build:
18
+ runs-on: ubuntu-latest
19
+ steps:
20
+ - uses: actions/checkout@v4
21
+ - uses: astral-sh/setup-uv@v3
22
+ with:
23
+ version: "latest"
24
+ - run: uv sync --group dev
25
+ - run: uv run ruff check .
26
+ - run: uv run pytest
27
+ - run: uv build
28
+ - uses: actions/upload-artifact@v4
29
+ with:
30
+ name: dist
31
+ path: dist/
32
+
33
+ publish:
34
+ needs: build
35
+ runs-on: ubuntu-latest
36
+ environment: pypi
37
+ permissions:
38
+ id-token: write # required for OIDC trusted publishing
39
+ steps:
40
+ - uses: actions/download-artifact@v4
41
+ with:
42
+ name: dist
43
+ path: dist/
44
+ - name: Publish to PyPI
45
+ uses: pypa/gh-action-pypi-publish@release/v1
46
+ with:
47
+ packages-dir: dist/
48
+ # No `password:` — uses GitHub OIDC + PyPI's trusted publisher.
@@ -0,0 +1,9 @@
1
+ .venv/
2
+ __pycache__/
3
+ .pytest_cache/
4
+ .ruff_cache/
5
+ dist/
6
+ *.egg-info/
7
+ .coverage
8
+ coverage.xml
9
+ htmlcov/
@@ -0,0 +1,174 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including the
49
+ original version of the Work and any modifications or additions to
50
+ that Work or Derivative Works thereof, that is intentionally submitted
51
+ to Licensor for inclusion in the Work by the copyright owner or by an
52
+ individual or Legal Entity authorized to submit on behalf of the
53
+ copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of, publicly
70
+ display, publicly perform, sublicense, and distribute the Work and such
71
+ Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of this
74
+ License, each Contributor hereby grants to You a perpetual, worldwide,
75
+ non-exclusive, no-charge, royalty-free, irrevocable (except as stated
76
+ in this section) patent license to make, have made, use, offer to sell,
77
+ sell, import, and otherwise transfer the Work, where such license
78
+ applies only to those patent claims licensable by such Contributor
79
+ that are necessarily infringed by their Contribution(s) alone or by
80
+ combination of their Contribution(s) with the Work to which such
81
+ Contribution(s) was submitted. If You institute patent litigation
82
+ against any entity (including a cross-claim or counterclaim in a
83
+ lawsuit) alleging that the Work or a Contribution incorporated within
84
+ the Work constitutes direct or contributory patent infringement, then
85
+ any patent licenses granted to You under this License for that Work
86
+ shall terminate as of the date such litigation is filed.
87
+
88
+ 4. Redistribution. You may reproduce and distribute copies of the Work or
89
+ Derivative Works thereof in any medium, with or without modifications,
90
+ and in Source or Object form, provided that You meet the following
91
+ conditions:
92
+
93
+ (a) You must give any other recipients of the Work or Derivative Works
94
+ a copy of this License; and
95
+
96
+ (b) You must cause any modified files to carry prominent notices
97
+ stating that You changed the files; and
98
+
99
+ (c) You must retain, in the Source form of any Derivative Works that
100
+ You distribute, all copyright, patent, trademark, and attribution
101
+ notices from the Source form of the Work, excluding those notices
102
+ that do not pertain to any part of the Derivative Works; and
103
+
104
+ (d) If the Work includes a "NOTICE" text file as part of its
105
+ distribution, then any Derivative Works that You distribute must
106
+ include a readable copy of the attribution notices contained
107
+ within such NOTICE file, excluding those notices that do not
108
+ pertain to any part of the Derivative Works, in at least one of
109
+ the following places: within a NOTICE text file distributed as
110
+ part of the Derivative Works; within the Source form or
111
+ documentation, if provided along with the Derivative Works; or,
112
+ within a display generated by the Derivative Works, if and
113
+ wherever such third-party notices normally appear. The contents
114
+ of the NOTICE file are for informational purposes only and do not
115
+ modify the License. You may add Your own attribution notices
116
+ within Derivative Works that You distribute, alongside or as an
117
+ addendum to the NOTICE text from the Work, provided that such
118
+ additional attribution notices cannot be construed as modifying
119
+ the License.
120
+
121
+ You may add Your own copyright statement to Your modifications and
122
+ may provide additional or different license terms and conditions for
123
+ use, reproduction, or distribution of Your modifications, or for any
124
+ such Derivative Works as a whole, provided Your use, reproduction, and
125
+ distribution of the Work otherwise complies with the conditions stated
126
+ in this License.
127
+
128
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
129
+ any Contribution intentionally submitted for inclusion in the Work by
130
+ You to the Licensor shall be under the terms and conditions of this
131
+ License, without any additional terms or conditions. Notwithstanding
132
+ the above, nothing herein shall supersede or modify the terms of any
133
+ separate license agreement you may have executed with Licensor
134
+ regarding such Contributions.
135
+
136
+ 6. Trademarks. This License does not grant permission to use the trade
137
+ names, trademarks, service marks, or product names of the Licensor,
138
+ except as required for reasonable and customary use in describing the
139
+ origin of the Work and reproducing the content of the NOTICE file.
140
+
141
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to
142
+ in writing, Licensor provides the Work (and each Contributor provides
143
+ its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR
144
+ CONDITIONS OF ANY KIND, either express or implied, including, without
145
+ limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT,
146
+ MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely
147
+ responsible for determining the appropriateness of using or
148
+ redistributing the Work and assume any risks associated with Your
149
+ exercise of permissions under this License.
150
+
151
+ 8. Limitation of Liability. In no event and under no legal theory,
152
+ whether in tort (including negligence), contract, or otherwise, unless
153
+ required by applicable law (such as deliberate and grossly negligent
154
+ acts) or agreed to in writing, shall any Contributor be liable to You
155
+ for damages, including any direct, indirect, special, incidental, or
156
+ consequential damages of any character arising as a result of this
157
+ License or out of the use or inability to use the Work (including but
158
+ not limited to damages for loss of goodwill, work stoppage, computer
159
+ failure or malfunction, or any and all other commercial damages or
160
+ losses), even if such Contributor has been advised of the possibility
161
+ of such damages.
162
+
163
+ 9. Accepting Warranty or Additional Liability. While redistributing the
164
+ Work or Derivative Works thereof, You may choose to offer, and charge
165
+ a fee for, acceptance of support, warranty, indemnity, or other
166
+ liability obligations and/or rights consistent with this License.
167
+ However, in accepting such obligations, You may act only on Your own
168
+ behalf and on Your sole responsibility, not on behalf of any other
169
+ Contributor, and only if You agree to indemnify, defend, and hold each
170
+ Contributor harmless for any liability incurred by, or claims asserted
171
+ against, such Contributor by reason of your accepting any such
172
+ warranty or additional liability.
173
+
174
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,282 @@
1
+ Metadata-Version: 2.4
2
+ Name: trusted-router-py
3
+ Version: 0.2.0
4
+ Summary: Python SDK for TrustedRouter.
5
+ Project-URL: Homepage, https://trustedrouter.com
6
+ Project-URL: Repository, https://github.com/Lore-Hex/trusted-router-py
7
+ Project-URL: Trust, https://trust.trustedrouter.com
8
+ Author: Lore Hex
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Requires-Python: >=3.10
17
+ Requires-Dist: httpx>=0.27.0
18
+ Provides-Extra: attestation
19
+ Requires-Dist: cryptography>=42; extra == 'attestation'
20
+ Description-Content-Type: text/markdown
21
+
22
+ # TrustedRouter Python SDK
23
+
24
+ OpenAI-compatible Python client for [TrustedRouter](https://trustedrouter.com) —
25
+ the hosted, attested LLM router that lets you point one OpenAI-shaped client
26
+ at every provider (Anthropic, OpenAI, Google Vertex, Gemini, DeepSeek,
27
+ Mistral, Cerebras) and *prove* the prompt path doesn't log.
28
+
29
+ - Gateway: `https://api.quillrouter.com/v1`
30
+ - Trust release: `https://trust.trustedrouter.com`
31
+ - Source: `https://github.com/Lore-Hex/trusted-router-py`
32
+ - License: Apache-2.0
33
+
34
+ ```bash
35
+ pip install trusted-router-py # base client
36
+ pip install trusted-router-py[attestation] # + GCP attestation verification
37
+ ```
38
+
39
+ ## Quick start
40
+
41
+ ```python
42
+ from trustedrouter import TrustedRouter, AUTO_MODEL
43
+
44
+ with TrustedRouter(api_key="sk-tr-v1-...") as client:
45
+ resp = client.chat_completions(
46
+ model=AUTO_MODEL, # "trustedrouter/auto" — multi-provider failover
47
+ messages=[{"role": "user", "content": "hello"}],
48
+ )
49
+ print(resp["choices"][0]["message"]["content"])
50
+ ```
51
+
52
+ `chat_completions(...)` defaults to `AUTO_MODEL` when `model=` is omitted, so
53
+ the simplest possible call is `client.chat_completions(messages=[...])`.
54
+
55
+ ## Streaming
56
+
57
+ ```python
58
+ for token in client.chat_completions_stream(
59
+ model=AUTO_MODEL,
60
+ messages=[{"role": "user", "content": "Write a haiku"}],
61
+ ):
62
+ print(token, end="", flush=True)
63
+ ```
64
+
65
+ `chat_completions_chunk_stream(...)` yields the raw OpenAI
66
+ `chat.completion.chunk` dicts (with `finish_reason`, `model`, `id`) when you
67
+ need more than just the text delta.
68
+
69
+ ## Async
70
+
71
+ Every method on `TrustedRouter` is mirrored on `AsyncTrustedRouter` as a
72
+ coroutine; streaming methods return `AsyncIterator`s. Use it from FastAPI,
73
+ asyncio, or any event-loop-driven app:
74
+
75
+ ```python
76
+ import asyncio
77
+ from trustedrouter import AsyncTrustedRouter
78
+
79
+ async def main():
80
+ async with AsyncTrustedRouter(api_key="sk-tr-v1-...") as client:
81
+ async for token in client.chat_completions_stream(
82
+ model="trustedrouter/auto",
83
+ messages=[{"role": "user", "content": "hi"}],
84
+ ):
85
+ print(token, end="", flush=True)
86
+
87
+ asyncio.run(main())
88
+ ```
89
+
90
+ ## Region pinning
91
+
92
+ The gateway is deployed in `us-central1` (the apex) and `europe-west4`. Pin
93
+ to a specific region with one kwarg — no need to construct the URL yourself:
94
+
95
+ ```python
96
+ client = TrustedRouter(api_key="sk-tr-v1-...", region="europe-west4")
97
+ ```
98
+
99
+ The full list lives in `trustedrouter.REGION_HOSTS`. Pass `region=` for known
100
+ regions, or `base_url=` for a custom endpoint (e.g. a self-hosted gateway).
101
+ Passing both is a configuration error.
102
+
103
+ ## Typed errors
104
+
105
+ Every HTTP failure raises a typed subclass of `TrustedRouterError` so callers
106
+ can discriminate without inspecting status codes:
107
+
108
+ ```python
109
+ from trustedrouter import (
110
+ TrustedRouter, AuthenticationError, RateLimitError,
111
+ BadRequestError, NotFoundError, InternalError,
112
+ )
113
+
114
+ try:
115
+ client.chat_completions(messages=[...])
116
+ except RateLimitError as e:
117
+ time.sleep(e.retry_after or 5) # honors Retry-After header
118
+ except AuthenticationError:
119
+ refresh_key()
120
+ except BadRequestError as e:
121
+ log.warning("bad request: %s", e)
122
+ except InternalError:
123
+ pass # auto-retried; still failing
124
+ ```
125
+
126
+ All subclasses inherit from `TrustedRouterError`, so existing
127
+ `except TrustedRouterError` blocks keep working.
128
+
129
+ ## Automatic retries
130
+
131
+ By default the client retries `429` and `5xx` responses up to **2 times**
132
+ with exponential backoff + jitter (capped at 30s, honors `Retry-After`).
133
+ Disable with `max_retries=0`:
134
+
135
+ ```python
136
+ client = TrustedRouter(api_key="...", max_retries=0) # raise immediately on transient
137
+ ```
138
+
139
+ ## Per-call extras
140
+
141
+ Every chat method (and `request()` for ad-hoc paths) accepts:
142
+
143
+ | kwarg | use |
144
+ |---|---|
145
+ | `api_key=` | override the instance bearer for this call only (threadsafe — used by validate_bearer) |
146
+ | `extra_headers=` | dict of headers to merge in (trace IDs, custom routing) |
147
+ | `idempotency_key=` | adds `Idempotency-Key:` so the gateway dedupes retries — **strongly recommended for billing** |
148
+ | `timeout=` | override the client-level timeout for this call |
149
+
150
+ ```python
151
+ client.billing_checkout(
152
+ amount=25,
153
+ payment_method="stablecoin",
154
+ idempotency_key=f"checkout-{user_id}-{order_id}", # never double-charge
155
+ )
156
+ ```
157
+
158
+ ## Attestation verification (the differentiator)
159
+
160
+ Every TrustedRouter response is generated inside a Google Confidential Space
161
+ workload. The gateway's `/attestation` endpoint mints a Google-signed JWT
162
+ that commits to the workload image digest, image reference, your nonce, and
163
+ the TLS leaf cert SHA-256. Verifying it proves the prompt path you're about
164
+ to use is the exact build the trust page advertises:
165
+
166
+ ```python
167
+ import secrets, ssl, socket
168
+ from trustedrouter import TrustedRouter
169
+ from trustedrouter.attestation import (
170
+ verify_gateway_attestation, policy_from_trust_release,
171
+ )
172
+
173
+ # Pull the published image digest/reference from the trust page
174
+ policy = policy_from_trust_release() # or pin one explicitly
175
+
176
+ with TrustedRouter(api_key="sk-tr-v1-...") as client:
177
+ nonce = secrets.token_hex(16)
178
+ jwt = client.attestation() # raw JWT bytes
179
+
180
+ # Bind the JWT to the live TLS connection's cert
181
+ with ssl.create_default_context().wrap_socket(
182
+ socket.create_connection(("api.quillrouter.com", 443)),
183
+ server_hostname="api.quillrouter.com",
184
+ ) as s:
185
+ cert_der = s.getpeercert(binary_form=True)
186
+
187
+ attestation = verify_gateway_attestation(
188
+ jwt, policy=policy, nonce_hex=nonce, tls_cert_der=cert_der
189
+ )
190
+ print("verified gateway:", attestation.image_digest)
191
+ ```
192
+
193
+ `verify_gateway_attestation()` raises `AttestationVerificationError` on any
194
+ of: bad signature, expired JWT, wrong issuer, audience mismatch,
195
+ image_digest mismatch, image_reference mismatch, missing nonce echo, or
196
+ TLS cert mismatch. Never returns falsey for a failed verification.
197
+
198
+ This codepath needs `cryptography`; install with
199
+ `pip install trusted-router-py[attestation]`.
200
+
201
+ ## Bring your own httpx client
202
+
203
+ Pass `client=` if you need a custom transport (cert pinning, retries you
204
+ manage, observability hooks). The SDK won't close it on `aclose()`:
205
+
206
+ ```python
207
+ import httpx
208
+ from trustedrouter import AsyncTrustedRouter
209
+
210
+ my_client = httpx.AsyncClient(
211
+ timeout=30.0,
212
+ event_hooks={"response": [my_cert_pin_hook]},
213
+ )
214
+ sdk = AsyncTrustedRouter(api_key="...", client=my_client)
215
+ # ...use sdk...
216
+ await sdk.aclose() # no-op for caller-owned clients
217
+ await my_client.aclose() # caller still owns lifecycle
218
+ ```
219
+
220
+ This is exactly how the [Quill device](https://github.com/Lore-Hex/quill)
221
+ wraps the SDK so it can pin Quill Cloud's self-signed leaf cert via an
222
+ httpx event hook, while delegating chat streaming to the SDK.
223
+
224
+ ## CLI
225
+
226
+ `pip install` exposes a `trustedrouter` console script for sniff tests:
227
+
228
+ ```bash
229
+ export TRUSTEDROUTER_API_KEY=sk-tr-v1-...
230
+
231
+ trustedrouter chat "hello" # one-shot completion
232
+ trustedrouter chat --stream "long answer" # token-by-token
233
+ trustedrouter regions # list deployed regions
234
+ trustedrouter providers # list provider catalog
235
+ trustedrouter models # list model catalog
236
+ trustedrouter trust # show trust release
237
+ trustedrouter attest # raw JWT bytes (pipe to `jq`-able tools)
238
+ trustedrouter --region europe-west4 chat "hi"
239
+ ```
240
+
241
+ ## Other endpoints
242
+
243
+ ```python
244
+ client.models() # OpenAI-shape catalog
245
+ client.providers() # provider list
246
+ client.regions() # deployed regions
247
+ client.credits() # current prepaid balance
248
+ client.activity(since="2026-01-01", limit=50)
249
+ client.embeddings(model="text-embed", input="hello")
250
+ client.messages( # Anthropic-shape, preserves system + content blocks
251
+ model="anthropic/claude-3-5-sonnet",
252
+ messages=[{"role": "user", "content": "hi"}],
253
+ max_tokens=512,
254
+ )
255
+ client.billing_checkout(amount=25, payment_method="stablecoin", idempotency_key=...)
256
+ ```
257
+
258
+ For routes the SDK doesn't wrap, drop down to `client.request(...)`:
259
+
260
+ ```python
261
+ client.request("GET", "/some/new/route", headers={"x-trace": "abc"})
262
+ ```
263
+
264
+ ## Roadmap
265
+
266
+ - **v0.3 (planned):** typed pydantic response models. Today every method
267
+ returns `dict[str, Any]`; pydantic models will give you IDE autocomplete +
268
+ runtime validation. Currently held back to avoid adding a heavy dependency
269
+ to the base install.
270
+ - **v0.x:** AWS Nitro Enclaves attestation path (currently only GCP).
271
+
272
+ ## Contributing
273
+
274
+ ```bash
275
+ uv sync --group dev
276
+ uv run ruff check .
277
+ uv run pytest # ~110 tests, ≥85% coverage gate
278
+ ```
279
+
280
+ CI runs lint + tests on every push to main and PR. Coverage gate is
281
+ enforced — PRs that drop coverage below 85% fail. Add tests with new
282
+ public surface.