gentrail 0.5.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.
- gentrail-0.5.0/.gitignore +8 -0
- gentrail-0.5.0/LICENSE +202 -0
- gentrail-0.5.0/PKG-INFO +193 -0
- gentrail-0.5.0/README.md +165 -0
- gentrail-0.5.0/gentrail/__init__.py +40 -0
- gentrail-0.5.0/gentrail/enforcement.py +299 -0
- gentrail-0.5.0/gentrail/evidence_ledger.py +81 -0
- gentrail-0.5.0/gentrail/hooks.py +315 -0
- gentrail-0.5.0/gentrail/init.py +50 -0
- gentrail-0.5.0/gentrail/langchain.py +138 -0
- gentrail-0.5.0/gentrail/openai_agents.py +102 -0
- gentrail-0.5.0/gentrail/otel_exporter.py +394 -0
- gentrail-0.5.0/gentrail/processor.py +139 -0
- gentrail-0.5.0/pyproject.toml +44 -0
- gentrail-0.5.0/tests/test_enforcement.py +224 -0
- gentrail-0.5.0/tests/test_enforcement_async.py +168 -0
- gentrail-0.5.0/tests/test_enforcement_attribute.py +79 -0
- gentrail-0.5.0/tests/test_env_gate.py +85 -0
- gentrail-0.5.0/tests/test_init.py +165 -0
- gentrail-0.5.0/tests/test_langchain_adapter.py +213 -0
- gentrail-0.5.0/tests/test_openai_agents_adapter.py +203 -0
- gentrail-0.5.0/tests/test_processor.py +172 -0
- gentrail-0.5.0/tests/test_redaction.py +92 -0
- gentrail-0.5.0/uv.lock +2477 -0
gentrail-0.5.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
gentrail-0.5.0/PKG-INFO
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: gentrail
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: Gentrail SDK: governance hooks, inline policy enforcement, and telemetry capture for AI agents
|
|
5
|
+
Project-URL: Homepage, https://gentrail.ai
|
|
6
|
+
Project-URL: Repository, https://github.com/aigentrail/sdk
|
|
7
|
+
Project-URL: Issues, https://github.com/aigentrail/sdk/issues
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: ai-agents,compliance,governance,opentelemetry,strands
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: System :: Monitoring
|
|
16
|
+
Requires-Python: >=3.9
|
|
17
|
+
Requires-Dist: opentelemetry-api>=1.20.0
|
|
18
|
+
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.20.0
|
|
19
|
+
Requires-Dist: opentelemetry-sdk>=1.20.0
|
|
20
|
+
Requires-Dist: pydantic>=2.0
|
|
21
|
+
Provides-Extra: langchain
|
|
22
|
+
Requires-Dist: langchain>=1.0; (python_version >= '3.10') and extra == 'langchain'
|
|
23
|
+
Provides-Extra: openai-agents
|
|
24
|
+
Requires-Dist: openai-agents>=0.4; (python_version >= '3.10') and extra == 'openai-agents'
|
|
25
|
+
Provides-Extra: strands
|
|
26
|
+
Requires-Dist: strands-agents>=0.1.0; (python_version >= '3.10') and extra == 'strands'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# gentrail
|
|
30
|
+
|
|
31
|
+
Governance SDK for AI agents. It captures compliance-evidence-grade telemetry
|
|
32
|
+
from an agent run and, when enabled, enforces policy inline before a tool call
|
|
33
|
+
executes.
|
|
34
|
+
|
|
35
|
+
A Go SDK with the same span shape lives in [`../go/`](../go/) (`go get
|
|
36
|
+
github.com/aigentrail/sdk/go`).
|
|
37
|
+
|
|
38
|
+
## Quickstart
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install "gentrail[strands]"
|
|
42
|
+
export GENTRAIL_API_KEY="sk-..."
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import gentrail
|
|
47
|
+
from strands import Agent
|
|
48
|
+
|
|
49
|
+
g = gentrail.init()
|
|
50
|
+
agent = Agent(model=model, tools=tools, hooks=[g.hook()])
|
|
51
|
+
agent("Reconcile the Q3 invoices")
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
That is the whole integration. `init()` reads the environment, builds the
|
|
55
|
+
governance tracer and the policy enforcer, and `hook()` returns a Strands
|
|
56
|
+
`HookProvider` that captures prompts, chain-of-thought, and tool calls, ships
|
|
57
|
+
them over OTLP, registers the agent on its first invocation, and enforces
|
|
58
|
+
verdicts before a tool runs. Use one `hook()` per agent. Whatever is not
|
|
59
|
+
configured stays off: with no API key the agent still runs, capture-only.
|
|
60
|
+
|
|
61
|
+
### OpenAI Agents SDK
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install "gentrail[openai-agents]"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from agents import function_tool
|
|
69
|
+
from gentrail.openai_agents import enforcement_guardrail
|
|
70
|
+
|
|
71
|
+
@function_tool(tool_input_guardrails=[enforcement_guardrail()])
|
|
72
|
+
def run_sql(database: str, sql: str) -> str: ...
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
A tool input guardrail that asks the decide endpoint before the tool runs.
|
|
76
|
+
BLOCK rejects the call with the policy message; GATE holds it until a human
|
|
77
|
+
approves in the Gentrail dashboard.
|
|
78
|
+
|
|
79
|
+
### LangChain / LangGraph
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
pip install "gentrail[langchain]"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from langchain.agents import create_agent
|
|
87
|
+
from gentrail.langchain import enforcement_middleware
|
|
88
|
+
|
|
89
|
+
agent = create_agent(
|
|
90
|
+
model=model,
|
|
91
|
+
tools=tools,
|
|
92
|
+
middleware=[enforcement_middleware(agent_id="reporter")],
|
|
93
|
+
)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Agent middleware wrapping every tool call, sync and async. Same verdicts: a
|
|
97
|
+
blocked or unapproved call becomes an error `ToolMessage` and the tool never
|
|
98
|
+
executes.
|
|
99
|
+
|
|
100
|
+
## Bring your own telemetry
|
|
101
|
+
|
|
102
|
+
Frameworks that emit OpenTelemetry GenAI telemetry natively (Strands with
|
|
103
|
+
`strands-agents[otel]`, Google ADK, Pydantic AI, LangChain with
|
|
104
|
+
`LANGSMITH_OTEL_ENABLED`, the Vercel AI SDK) do not need the SDK to build
|
|
105
|
+
spans: the Gentrail backend ingests `gen_ai.*`, `ai.*`, and OpenInference
|
|
106
|
+
telemetry directly. This is the recommended integration for those frameworks.
|
|
107
|
+
What the SDK still adds is client-side PII redaction and export to Gentrail:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
import gentrail
|
|
111
|
+
|
|
112
|
+
gentrail.instrument()
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`instrument()` attaches a `GentrailSpanProcessor` to your application's
|
|
116
|
+
`TracerProvider` (pass yours with `instrument(provider=...)`; a fresh one is
|
|
117
|
+
installed only when none exists). The processor redacts PII from `gen_ai.*`,
|
|
118
|
+
`ai.*`, and OpenInference input/output attributes before any value leaves the
|
|
119
|
+
process, stamps `aigentrail.redaction.applied` on spans it changed, and ships
|
|
120
|
+
them to the Gentrail collector over OTLP. Other exporters on the provider keep
|
|
121
|
+
the raw spans. Without `GENTRAIL_API_KEY` it returns `None` and the app runs
|
|
122
|
+
unchanged.
|
|
123
|
+
|
|
124
|
+
Inline enforcement stays separate: add the enforcement adapter for your
|
|
125
|
+
framework (above) to get BLOCK and GATE verdicts before a tool runs.
|
|
126
|
+
|
|
127
|
+
## Configuration
|
|
128
|
+
|
|
129
|
+
All through environment variables:
|
|
130
|
+
|
|
131
|
+
- `GENTRAIL_API_KEY`: enables OTLP export of governance spans.
|
|
132
|
+
- `OTEL_EXPORTER_OTLP_ENDPOINT`: collector base URL, default
|
|
133
|
+
`https://otel.gentrail.ai`.
|
|
134
|
+
- `OTEL_EXPORTER_OTLP_HEADERS`: standard OTel header list; when set it replaces
|
|
135
|
+
the SDK's default `Authorization: Bearer <GENTRAIL_API_KEY>` header.
|
|
136
|
+
- `GENTRAIL_DECIDE_ENDPOINT`: enables inline enforcement (see below).
|
|
137
|
+
- `GENTRAIL_REDACT_PII`: set to `false` to disable client-side PII redaction.
|
|
138
|
+
- `GENTRAIL_GATE_TIMEOUT_SECONDS`: how long a gated tool call waits for human
|
|
139
|
+
approval, default 120.
|
|
140
|
+
|
|
141
|
+
If your application already configures an OpenTelemetry `TracerProvider`, the
|
|
142
|
+
SDK attaches its exporter to it as an extra span processor instead of replacing
|
|
143
|
+
it; a fresh provider is installed only when none exists.
|
|
144
|
+
|
|
145
|
+
## PII redaction
|
|
146
|
+
|
|
147
|
+
The SDK redacts high-confidence PII (emails, SSNs, credit cards, AWS keys)
|
|
148
|
+
from span input and output values before they leave the process, replacing each
|
|
149
|
+
with a typed placeholder like `[EMAIL]`, so the raw value never reaches the
|
|
150
|
+
collector while the data class stays visible for governance. This applies to
|
|
151
|
+
spans the SDK builds and, via `GentrailSpanProcessor`, to `gen_ai.*`, `ai.*`,
|
|
152
|
+
and OpenInference attributes on spans your framework emits itself. On by
|
|
153
|
+
default; opt out with `GENTRAIL_REDACT_PII=false` or `instrument(redact=False)`.
|
|
154
|
+
|
|
155
|
+
## Inline enforcement (opt-in)
|
|
156
|
+
|
|
157
|
+
The async backend evaluator only sees a trace after a tool has already run, so
|
|
158
|
+
it can detect but never prevent. Enforcement happens here, in the before-tool-call
|
|
159
|
+
hook: the SDK asks the backend for a verdict on the proposed tool call and stops
|
|
160
|
+
it before it executes.
|
|
161
|
+
|
|
162
|
+
It is opt-in and fails open. Set both environment variables to turn it on:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
export GENTRAIL_DECIDE_ENDPOINT="https://your-dashboard.example"
|
|
166
|
+
export GENTRAIL_API_KEY="sk-..."
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
With these set, a `BLOCK` verdict cancels the tool call (the agent receives an
|
|
170
|
+
error tool result and the tool never runs). A `GATE` verdict also stops the call
|
|
171
|
+
pending human approval. A backend error never breaks the agent: the call is
|
|
172
|
+
allowed and enforcement is skipped for that step.
|
|
173
|
+
|
|
174
|
+
## Advanced: raw capture surfaces
|
|
175
|
+
|
|
176
|
+
`init()` and `hook()` compose lower-level pieces that remain importable for
|
|
177
|
+
consumers that need them directly:
|
|
178
|
+
|
|
179
|
+
- `evidence_ledger.py`: a local append-only audit log with integrity hashes;
|
|
180
|
+
the hook seals one `DecisionJournal` per invocation.
|
|
181
|
+
- `otel_exporter.py`: `create_governance_tracer()` / `get_governance_tracer()`
|
|
182
|
+
build the OTLP pipeline without the rest of the SDK.
|
|
183
|
+
- `enforcement.py`: `PolicyEnforcer` and its asyncio twin `AsyncPolicyEnforcer`
|
|
184
|
+
are the raw decide/gate clients.
|
|
185
|
+
|
|
186
|
+
## Develop
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
uv sync --extra strands
|
|
190
|
+
ruff check .
|
|
191
|
+
python3 tests/test_enforcement.py
|
|
192
|
+
python3 tests/test_init.py
|
|
193
|
+
```
|
gentrail-0.5.0/README.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# gentrail
|
|
2
|
+
|
|
3
|
+
Governance SDK for AI agents. It captures compliance-evidence-grade telemetry
|
|
4
|
+
from an agent run and, when enabled, enforces policy inline before a tool call
|
|
5
|
+
executes.
|
|
6
|
+
|
|
7
|
+
A Go SDK with the same span shape lives in [`../go/`](../go/) (`go get
|
|
8
|
+
github.com/aigentrail/sdk/go`).
|
|
9
|
+
|
|
10
|
+
## Quickstart
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install "gentrail[strands]"
|
|
14
|
+
export GENTRAIL_API_KEY="sk-..."
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import gentrail
|
|
19
|
+
from strands import Agent
|
|
20
|
+
|
|
21
|
+
g = gentrail.init()
|
|
22
|
+
agent = Agent(model=model, tools=tools, hooks=[g.hook()])
|
|
23
|
+
agent("Reconcile the Q3 invoices")
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
That is the whole integration. `init()` reads the environment, builds the
|
|
27
|
+
governance tracer and the policy enforcer, and `hook()` returns a Strands
|
|
28
|
+
`HookProvider` that captures prompts, chain-of-thought, and tool calls, ships
|
|
29
|
+
them over OTLP, registers the agent on its first invocation, and enforces
|
|
30
|
+
verdicts before a tool runs. Use one `hook()` per agent. Whatever is not
|
|
31
|
+
configured stays off: with no API key the agent still runs, capture-only.
|
|
32
|
+
|
|
33
|
+
### OpenAI Agents SDK
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install "gentrail[openai-agents]"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from agents import function_tool
|
|
41
|
+
from gentrail.openai_agents import enforcement_guardrail
|
|
42
|
+
|
|
43
|
+
@function_tool(tool_input_guardrails=[enforcement_guardrail()])
|
|
44
|
+
def run_sql(database: str, sql: str) -> str: ...
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
A tool input guardrail that asks the decide endpoint before the tool runs.
|
|
48
|
+
BLOCK rejects the call with the policy message; GATE holds it until a human
|
|
49
|
+
approves in the Gentrail dashboard.
|
|
50
|
+
|
|
51
|
+
### LangChain / LangGraph
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install "gentrail[langchain]"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from langchain.agents import create_agent
|
|
59
|
+
from gentrail.langchain import enforcement_middleware
|
|
60
|
+
|
|
61
|
+
agent = create_agent(
|
|
62
|
+
model=model,
|
|
63
|
+
tools=tools,
|
|
64
|
+
middleware=[enforcement_middleware(agent_id="reporter")],
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Agent middleware wrapping every tool call, sync and async. Same verdicts: a
|
|
69
|
+
blocked or unapproved call becomes an error `ToolMessage` and the tool never
|
|
70
|
+
executes.
|
|
71
|
+
|
|
72
|
+
## Bring your own telemetry
|
|
73
|
+
|
|
74
|
+
Frameworks that emit OpenTelemetry GenAI telemetry natively (Strands with
|
|
75
|
+
`strands-agents[otel]`, Google ADK, Pydantic AI, LangChain with
|
|
76
|
+
`LANGSMITH_OTEL_ENABLED`, the Vercel AI SDK) do not need the SDK to build
|
|
77
|
+
spans: the Gentrail backend ingests `gen_ai.*`, `ai.*`, and OpenInference
|
|
78
|
+
telemetry directly. This is the recommended integration for those frameworks.
|
|
79
|
+
What the SDK still adds is client-side PII redaction and export to Gentrail:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
import gentrail
|
|
83
|
+
|
|
84
|
+
gentrail.instrument()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`instrument()` attaches a `GentrailSpanProcessor` to your application's
|
|
88
|
+
`TracerProvider` (pass yours with `instrument(provider=...)`; a fresh one is
|
|
89
|
+
installed only when none exists). The processor redacts PII from `gen_ai.*`,
|
|
90
|
+
`ai.*`, and OpenInference input/output attributes before any value leaves the
|
|
91
|
+
process, stamps `aigentrail.redaction.applied` on spans it changed, and ships
|
|
92
|
+
them to the Gentrail collector over OTLP. Other exporters on the provider keep
|
|
93
|
+
the raw spans. Without `GENTRAIL_API_KEY` it returns `None` and the app runs
|
|
94
|
+
unchanged.
|
|
95
|
+
|
|
96
|
+
Inline enforcement stays separate: add the enforcement adapter for your
|
|
97
|
+
framework (above) to get BLOCK and GATE verdicts before a tool runs.
|
|
98
|
+
|
|
99
|
+
## Configuration
|
|
100
|
+
|
|
101
|
+
All through environment variables:
|
|
102
|
+
|
|
103
|
+
- `GENTRAIL_API_KEY`: enables OTLP export of governance spans.
|
|
104
|
+
- `OTEL_EXPORTER_OTLP_ENDPOINT`: collector base URL, default
|
|
105
|
+
`https://otel.gentrail.ai`.
|
|
106
|
+
- `OTEL_EXPORTER_OTLP_HEADERS`: standard OTel header list; when set it replaces
|
|
107
|
+
the SDK's default `Authorization: Bearer <GENTRAIL_API_KEY>` header.
|
|
108
|
+
- `GENTRAIL_DECIDE_ENDPOINT`: enables inline enforcement (see below).
|
|
109
|
+
- `GENTRAIL_REDACT_PII`: set to `false` to disable client-side PII redaction.
|
|
110
|
+
- `GENTRAIL_GATE_TIMEOUT_SECONDS`: how long a gated tool call waits for human
|
|
111
|
+
approval, default 120.
|
|
112
|
+
|
|
113
|
+
If your application already configures an OpenTelemetry `TracerProvider`, the
|
|
114
|
+
SDK attaches its exporter to it as an extra span processor instead of replacing
|
|
115
|
+
it; a fresh provider is installed only when none exists.
|
|
116
|
+
|
|
117
|
+
## PII redaction
|
|
118
|
+
|
|
119
|
+
The SDK redacts high-confidence PII (emails, SSNs, credit cards, AWS keys)
|
|
120
|
+
from span input and output values before they leave the process, replacing each
|
|
121
|
+
with a typed placeholder like `[EMAIL]`, so the raw value never reaches the
|
|
122
|
+
collector while the data class stays visible for governance. This applies to
|
|
123
|
+
spans the SDK builds and, via `GentrailSpanProcessor`, to `gen_ai.*`, `ai.*`,
|
|
124
|
+
and OpenInference attributes on spans your framework emits itself. On by
|
|
125
|
+
default; opt out with `GENTRAIL_REDACT_PII=false` or `instrument(redact=False)`.
|
|
126
|
+
|
|
127
|
+
## Inline enforcement (opt-in)
|
|
128
|
+
|
|
129
|
+
The async backend evaluator only sees a trace after a tool has already run, so
|
|
130
|
+
it can detect but never prevent. Enforcement happens here, in the before-tool-call
|
|
131
|
+
hook: the SDK asks the backend for a verdict on the proposed tool call and stops
|
|
132
|
+
it before it executes.
|
|
133
|
+
|
|
134
|
+
It is opt-in and fails open. Set both environment variables to turn it on:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
export GENTRAIL_DECIDE_ENDPOINT="https://your-dashboard.example"
|
|
138
|
+
export GENTRAIL_API_KEY="sk-..."
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
With these set, a `BLOCK` verdict cancels the tool call (the agent receives an
|
|
142
|
+
error tool result and the tool never runs). A `GATE` verdict also stops the call
|
|
143
|
+
pending human approval. A backend error never breaks the agent: the call is
|
|
144
|
+
allowed and enforcement is skipped for that step.
|
|
145
|
+
|
|
146
|
+
## Advanced: raw capture surfaces
|
|
147
|
+
|
|
148
|
+
`init()` and `hook()` compose lower-level pieces that remain importable for
|
|
149
|
+
consumers that need them directly:
|
|
150
|
+
|
|
151
|
+
- `evidence_ledger.py`: a local append-only audit log with integrity hashes;
|
|
152
|
+
the hook seals one `DecisionJournal` per invocation.
|
|
153
|
+
- `otel_exporter.py`: `create_governance_tracer()` / `get_governance_tracer()`
|
|
154
|
+
build the OTLP pipeline without the rest of the SDK.
|
|
155
|
+
- `enforcement.py`: `PolicyEnforcer` and its asyncio twin `AsyncPolicyEnforcer`
|
|
156
|
+
are the raw decide/gate clients.
|
|
157
|
+
|
|
158
|
+
## Develop
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
uv sync --extra strands
|
|
162
|
+
ruff check .
|
|
163
|
+
python3 tests/test_enforcement.py
|
|
164
|
+
python3 tests/test_init.py
|
|
165
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Gentrail SDK: governance hooks, inline policy enforcement, and telemetry
|
|
2
|
+
capture for AI agents. Start with gentrail.init()."""
|
|
3
|
+
|
|
4
|
+
from .enforcement import AsyncPolicyEnforcer, PolicyEnforcer
|
|
5
|
+
from .init import Gentrail, init
|
|
6
|
+
from .otel_exporter import GovernanceTracer, create_governance_tracer, get_governance_tracer
|
|
7
|
+
from .processor import GentrailSpanProcessor, instrument
|
|
8
|
+
|
|
9
|
+
from .evidence_ledger import (
|
|
10
|
+
DecisionJournal,
|
|
11
|
+
EvidenceLedger,
|
|
12
|
+
ModelCallRecord,
|
|
13
|
+
ToolCallRecord,
|
|
14
|
+
evidence_ledger,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
# Strands HookProvider is optional: importing strands when it is not installed
|
|
18
|
+
# would break SDK consumers that only need the OTLP exporter (e.g. policy_engine).
|
|
19
|
+
try:
|
|
20
|
+
from .hooks import GentrailGovernanceHook
|
|
21
|
+
except ImportError:
|
|
22
|
+
GentrailGovernanceHook = None # type: ignore[assignment,misc]
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"init",
|
|
26
|
+
"instrument",
|
|
27
|
+
"Gentrail",
|
|
28
|
+
"GentrailGovernanceHook",
|
|
29
|
+
"GentrailSpanProcessor",
|
|
30
|
+
"AsyncPolicyEnforcer",
|
|
31
|
+
"PolicyEnforcer",
|
|
32
|
+
"GovernanceTracer",
|
|
33
|
+
"create_governance_tracer",
|
|
34
|
+
"get_governance_tracer",
|
|
35
|
+
"DecisionJournal",
|
|
36
|
+
"EvidenceLedger",
|
|
37
|
+
"ModelCallRecord",
|
|
38
|
+
"ToolCallRecord",
|
|
39
|
+
"evidence_ledger",
|
|
40
|
+
]
|