TrustGate Python SDK with Automatic Context for Enterprises
Project description
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. 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.
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file trustgate-0.1.0.tar.gz.
File metadata
- Download URL: trustgate-0.1.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
939f13176ae3662f64c21d5e47b3dca3fe0e6d4f0e3bb80661abd9b602d0b263
|
|
| MD5 |
439c0793500b17ee7a5b6293493e6e9e
|
|
| BLAKE2b-256 |
3dcfe9c77dac0a7769f9ba6b5557d04728970403f2b681f2951b9ad2dec183ce
|
File details
Details for the file trustgate-0.1.0-py3-none-any.whl.
File metadata
- Download URL: trustgate-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb1c47ae8a8ce4e1924f9998b826564db71dcb543b344d30dc9bd2bcc7c4bd0f
|
|
| MD5 |
0994a7c07debb4771df8a673dff4f4bf
|
|
| BLAKE2b-256 |
f7a921597304afe1cd2415f6664e447816a4132d11c619fe01ed1e47bf8bdb58
|