trustgate-python
TrustGate Python SDK with Automatic Context for enterprises. Use the same API as OpenAI while routing through the TrustGate gateway and auto-injecting trace and workflow context (n8n, GitHub Actions, GitLab CI).
Install
pip install -e .
Usage
1. Direct client (two-keyword API)
Use tg.chat.completions.create() with the same arguments as openai.chat.completions.create(); TrustGate headers are added automatically.
from trustgate import TrustGate
tg = TrustGate(
base_url="https://your-trustgate-gateway.example",
api_key="your-api-key", # optional
)
response = tg.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}],
temperature=0.7,
)
# response is the same shape as OpenAI (e.g. response["choices"][0]["message"]["content"])
2. Zero-config: patch all SDKs at once
Apply TrustGate headers (and optional gateway routing) to OpenAI, Anthropic, and Google Gemini in one call. Uses TRUSTGATE_BASE_URL (and provider-specific env vars) when base_url is not set.
import trustgate
trustgate.patch_all() # or patch_all(base_url="https://your-gateway.example")
# Then use openai, anthropic, or google.generativeai as usual — all requests get x-trustgate-* headers
3. Monkey patch existing OpenAI code
Route all openai.OpenAI traffic through TrustGate and inject context headers:
import trustgate
trustgate.patch_openai(base_url="https://your-trustgate-gateway.example")
import openai
client = openai.OpenAI(api_key="...") # base_url and headers are overridden
resp = client.chat.completions.create(model="gpt-4o", messages=[...])
You can also set the gateway URL via environment:
export TRUSTGATE_BASE_URL=https://your-trustgate-gateway.example
Then call trustgate.patch_openai() with no arguments.
4. Multi-model: Anthropic and Gemini
All patches use the same context (context.build_trustgate_headers) for Shadow AI detection (e.g. local_script, n8n, GitHub Actions, GitLab CI).
Anthropic (optional dep: pip install trustgate[anthropic]):
import trustgate
trustgate.patch_anthropic(base_url="https://your-gateway.example")
import anthropic
client = anthropic.Anthropic(api_key="...") # base_url and x-trustgate-* headers set
Gemini — patch the new google.genai Client (optional dep: pip install trustgate[gemini]), or use the wrapper to send requests via the gateway:
import trustgate
trustgate.patch_google_generativeai(base_url="https://your-gateway.example")
# Or use the wrapper for full control (gateway must proxy to Gemini):
from trustgate import TrustGateGeminiClient
client = TrustGateGeminiClient(base_url="https://your-gateway.example", api_key="...")
response = client.generate_content(model="gemini-1.5-flash", contents="Hello")
Env vars: TRUSTGATE_ANTHROPIC_BASE_URL, TRUSTGATE_GEMINI_BASE_URL (fallback: TRUSTGATE_BASE_URL).
Automatic context (bridge headers)
The SDK detects the environment and sets:
x-trustgate-trace-id– Execution/pipeline id (e.g.N8N_EXECUTION_ID,GITHUB_RUN_ID,CI_PIPELINE_ID) or a generated UUID.x-trustgate-workflow-name– Workflow/pipeline name when available (n8n workflow, GitHub workflow, GitLab job).x-trustgate-workflow-step– Optional step name for granular traceability (see Granular traceability); in n8n can be auto-set fromN8N_NODE_ID/N8N_NODE_NAME.x-trustgate-source– One ofn8n,github_actions,gitlab_ci, orlocal_script(when no env is detected, for Shadow AI monitoring).
Detection is based on environment variables:
| Source | Env vars (examples) |
|---|---|
| n8n | N8N_EXECUTION_ID, N8N_WORKFLOW_NAME, N8N_NODE_ID, N8N_NODE_NAME |
| GitHub Actions | GITHUB_ACTIONS, GITHUB_RUN_ID, GITHUB_WORKFLOW |
| GitLab CI | GITLAB_CI, CI_PIPELINE_ID, CI_JOB_NAME |
Metadata
Every request includes source_tool metadata (in the x-trustgate-source-tool header as JSON):
sdk_version– TrustGate Python SDK versionpython_version– Python version (e.g.3.11.5)os– OS name (e.g.Windows,Linux)
You can add or override keys per request with source_tool_override:
tg.chat.completions.create(
model="gpt-4o",
messages=[...],
source_tool_override={"step_name": "extract", "stage": "preprocessing"},
)
Granular traceability
Use workflow_step so the gateway can show different parts of the same workflow (e.g. n8n) as separate steps in the Agent Intelligence Gantt chart. Without it, all calls in one run look like a single block; with it, you see segments like Data_Extraction, Final_Summary, etc.
Direct client:
tg.chat.completions.create(
model="gpt-4o",
messages=[...],
workflow_step="Data_Extraction",
)
# later in the same workflow
tg.chat.completions.create(
model="gpt-4o",
messages=[...],
workflow_step="Final_Summary",
)
n8n (automatic): When running inside n8n, the SDK can set x-trustgate-workflow-step automatically from N8N_NODE_ID or N8N_NODE_NAME, so each node appears as its own step in the Gantt without code changes.
Monkey patch with a default step:
trustgate.patch_openai(
base_url="https://your-gateway.example",
workflow_step="CI_CodeReview",
)
# every OpenAI call from this process will send that step
Development
pip install -e ".[dev]"
pytest
License
MIT
Metadata
Release files for trustgate 0.10.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| trustgate-0.10.0.tar.gz | 54.2 kB | With provenance | Details for trustgate-0.10.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trustgate-0.10.0-py3-none-any.whl | Python 3 | none | any | Details for trustgate-0.10.0-py3-none-any.whl |
Total release size: 105.4 kB
Release files / trustgate-0.10.0.tar.gz
| Download URL | trustgate-0.10.0.tar.gz |
|---|---|
| Size | 54.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ec793b4ad75613ee8fee02468255bf14cb0261f452cd00c645ccacc8722d5da1
|
|
BLAKE2b-256 checksum How to use checksums |
9c81e10f77383452c5251b01e3945d8771bd9ce727d5c248a09a68f129ecd985
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.
Transparency logRelease files / trustgate-0.10.0-py3-none-any.whl
| Download URL | trustgate-0.10.0-py3-none-any.whl |
|---|---|
| Size | 51.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56199484ee18284568491ffe62d550d35e997b370a98e5b1b3acf5bde9b3bb98
|
|
BLAKE2b-256 checksum How to use checksums |
cadfc7b51db53ebe4a945759a5f445f528c162a56a76cfcf90c456db4e9e10a1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.
Transparency logSecurity for trustgate 0.10.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for trustgate-0.10.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.