norcaster-langchain 0.1.1__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.
- norcaster_langchain-0.1.1/.gitignore +6 -0
- norcaster_langchain-0.1.1/LICENSE +202 -0
- norcaster_langchain-0.1.1/PKG-INFO +406 -0
- norcaster_langchain-0.1.1/README.md +389 -0
- norcaster_langchain-0.1.1/examples/agent.py +428 -0
- norcaster_langchain-0.1.1/pyproject.toml +31 -0
- norcaster_langchain-0.1.1/src/norcaster_langchain/__init__.py +26 -0
- norcaster_langchain-0.1.1/src/norcaster_langchain/_answer.py +195 -0
- norcaster_langchain-0.1.1/src/norcaster_langchain/_conversation.py +397 -0
- norcaster_langchain-0.1.1/src/norcaster_langchain/check.py +299 -0
- norcaster_langchain-0.1.1/src/norcaster_langchain/middleware.py +308 -0
- norcaster_langchain-0.1.1/test/stand_ins.py +347 -0
- norcaster_langchain-0.1.1/test/test_agent.py +443 -0
- norcaster_langchain-0.1.1/test/test_answer.py +650 -0
- norcaster_langchain-0.1.1/test/test_conversation.py +281 -0
- norcaster_langchain-0.1.1/test/test_example.py +91 -0
- norcaster_langchain-0.1.1/test/test_openai_wire.py +314 -0
|
@@ -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.
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: norcaster-langchain
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Norcaster check for LangChain: your agent asks Norcaster for a decision (allow, hide or block) before each call to the AI provider, and Norcaster keeps a record of every answer.
|
|
5
|
+
Project-URL: Homepage, https://www.norcaster.com
|
|
6
|
+
Author-email: "Norcaster (Navego AB)" <security@norcaster.com>
|
|
7
|
+
License: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: agents,ai-governance,audit-evidence,langchain,langgraph,middleware
|
|
10
|
+
Requires-Python: >=3.12
|
|
11
|
+
Requires-Dist: langchain<2,>=1.2.11
|
|
12
|
+
Requires-Dist: norcaster<0.2,>=0.1.2
|
|
13
|
+
Provides-Extra: test
|
|
14
|
+
Requires-Dist: langchain-openai<2,>=1.0.0; extra == 'test'
|
|
15
|
+
Requires-Dist: pytest>=7; extra == 'test'
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# norcaster-langchain
|
|
19
|
+
|
|
20
|
+
For an agent built with [LangChain](https://docs.langchain.com/oss/python/langchain/overview). Before
|
|
21
|
+
every call the agent makes to the AI provider, it asks Norcaster for a decision. Norcaster answers
|
|
22
|
+
from the app's rules and keeps a record of every answer. The agent then calls the provider itself:
|
|
23
|
+
your app is not re-pointed, and your provider keys stay where they are.
|
|
24
|
+
|
|
25
|
+
| Norcaster's decision | What the agent does |
|
|
26
|
+
|---|---|
|
|
27
|
+
| **allow** | Calls the provider with the conversation as it is. |
|
|
28
|
+
| **hide** | Calls the provider with the version Norcaster returned, with the sensitive data hidden. |
|
|
29
|
+
| **block** | Does not call the provider. The run stops with an error that names the decision and the record. |
|
|
30
|
+
|
|
31
|
+
It can also ask about the model's answer before the agent uses it: see [Answer checks](#answer-checks).
|
|
32
|
+
|
|
33
|
+
**Status:** new. Tested with LangChain 1.2.11 and 1.5.0.
|
|
34
|
+
|
|
35
|
+
## Set up
|
|
36
|
+
|
|
37
|
+
You need a Norcaster key with the `runs:write` scope and the id of the app in Norcaster.
|
|
38
|
+
|
|
39
|
+
1. Install the adapter into the Python environment that runs your agent. It brings the Norcaster
|
|
40
|
+
SDK (`norcaster`) with it:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install norcaster-langchain
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
2. Add the middleware to your agent. It is one entry in the list, placed last:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
import os
|
|
50
|
+
|
|
51
|
+
from langchain.agents import create_agent
|
|
52
|
+
from norcaster_langchain import NorcasterCheckMiddleware
|
|
53
|
+
from norcaster import NorcasterCloudClient
|
|
54
|
+
|
|
55
|
+
norcaster = NorcasterCloudClient(os.environ["NORCASTER_API_KEY"], "https://api.norcaster.com")
|
|
56
|
+
|
|
57
|
+
agent = create_agent(
|
|
58
|
+
model,
|
|
59
|
+
tools,
|
|
60
|
+
middleware=[NorcasterCheckMiddleware(norcaster, deployment_id="<the app's id>")],
|
|
61
|
+
)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
3. Run the agent once. The model's answer carries the record's id
|
|
65
|
+
(`message.response_metadata["norcaster"]["receipt_id"]`), and the same call is in the app's
|
|
66
|
+
records in Norcaster, marked **Checked**.
|
|
67
|
+
|
|
68
|
+
[`examples/agent.py`](examples/agent.py) is a small agent that shows allow, hide, block and what
|
|
69
|
+
happens when Norcaster can't be reached. Its provider is a stand-in inside the script, so only the
|
|
70
|
+
checks leave your machine.
|
|
71
|
+
|
|
72
|
+
**Put the middleware last.** LangChain runs the list from the outside in, so a middleware placed
|
|
73
|
+
after this one can change the conversation after it was checked.
|
|
74
|
+
|
|
75
|
+
## What the record shows
|
|
76
|
+
|
|
77
|
+
The app asked Norcaster before it called the provider.
|
|
78
|
+
|
|
79
|
+
What it does not show:
|
|
80
|
+
|
|
81
|
+
- **What the agent sent afterwards.** Norcaster does not see the provider call. The record holds a
|
|
82
|
+
SHA-256 of the conversation as the adapter sent it to Norcaster (the text and the tool calls, as
|
|
83
|
+
chat messages). It is not a hash of the bytes LangChain sent to the provider.
|
|
84
|
+
- **That every call was checked.** Only model calls that go through this middleware are. See below.
|
|
85
|
+
- **Anything about the provider's answer**, unless [answer checks](#answer-checks) are on. Then
|
|
86
|
+
the record of the answer shows your app asked about a text: not that the text was the
|
|
87
|
+
provider's answer, or what your app did with it afterwards.
|
|
88
|
+
|
|
89
|
+
If you need a record that nothing reached the provider unchecked, send the app's traffic through
|
|
90
|
+
the Norcaster gateway instead.
|
|
91
|
+
|
|
92
|
+
The conversation is sent to Norcaster to be checked. If your data may not leave your network, this
|
|
93
|
+
adapter does not change that; a Norcaster you host yourself does.
|
|
94
|
+
|
|
95
|
+
## What is checked
|
|
96
|
+
|
|
97
|
+
**Every model call of the agent, not only the first.** An agent that uses a tool three times in a
|
|
98
|
+
row makes four model calls, and each one is checked before it is made. Each check covers
|
|
99
|
+
everything that call sends:
|
|
100
|
+
|
|
101
|
+
- the system prompt and the user's messages;
|
|
102
|
+
- the agent's earlier answers;
|
|
103
|
+
- the arguments of tool calls from earlier steps;
|
|
104
|
+
- the results of tools, which is how a secret a tool read is stopped before it goes to the provider.
|
|
105
|
+
|
|
106
|
+
A streamed call is checked before the stream starts. Sync and async runs are checked the same way.
|
|
107
|
+
|
|
108
|
+
**What is not checked:**
|
|
109
|
+
|
|
110
|
+
- **The model's answer, and the tool calls it asks for, when it asks for them**, unless you turn
|
|
111
|
+
on [answer checks](#answer-checks). Without them a tool runs with the arguments the model wrote,
|
|
112
|
+
and Norcaster reads those arguments at the next model call, with the rest of the conversation,
|
|
113
|
+
after the tool has run.
|
|
114
|
+
- **Tool definitions, the model's name and its settings.**
|
|
115
|
+
- **The model's own reasoning** that some providers send back with the conversation. Providers
|
|
116
|
+
sign it, so it is sent on as it is and Norcaster is not asked about it.
|
|
117
|
+
- **Model calls made outside this middleware:** another middleware that calls a model itself
|
|
118
|
+
(summarization, tool selection), a sub-agent built without the middleware, a tool that calls a
|
|
119
|
+
model, a call your code makes directly.
|
|
120
|
+
- **An `extra_body` or `model_kwargs` chosen per run**, as a configurable field
|
|
121
|
+
(`init_chat_model(configurable_fields=...)`, `configurable_fields(...)`): the middleware can't
|
|
122
|
+
see it. Don't make either one configurable on a model the agent checks.
|
|
123
|
+
|
|
124
|
+
**What is refused.** A call that holds anything a check can't read is not sent: an image, a file,
|
|
125
|
+
audio, or the result of a tool the provider runs itself. The run stops with `NorcasterCannotCheck`
|
|
126
|
+
and Norcaster is not asked. This is so that nothing goes to the provider unread. An agent that
|
|
127
|
+
takes images can't use this adapter yet.
|
|
128
|
+
|
|
129
|
+
The same goes for a model whose settings carry content of their own:
|
|
130
|
+
- `messages`, `input`, `instructions` or `prompt` in `extra_body` (on the model, on a bound model,
|
|
131
|
+
or in a middleware's `model_settings`);
|
|
132
|
+
- `instructions` or a stored `prompt` set on the model;
|
|
133
|
+
- a predicted output (`prediction`, OpenAI's Predicted Outputs) in `model_kwargs`, in `extra_body`,
|
|
134
|
+
on a bound model or in a middleware's `model_settings`. It is usually the same file a message
|
|
135
|
+
holds, and a check doesn't read it, so a hide in that file would go out unhidden there.
|
|
136
|
+
|
|
137
|
+
The OpenAI client would send that beside or in place of the checked messages. Put it in the
|
|
138
|
+
agent's system prompt or messages instead, and leave `prediction` out.
|
|
139
|
+
|
|
140
|
+
### When the decision is hide
|
|
141
|
+
|
|
142
|
+
Norcaster's hidden text is written into a copy of the conversation, in every place it lives: the
|
|
143
|
+
message's text, the tool call's arguments, the tool's result. The provider gets the copy. The
|
|
144
|
+
conversation your app keeps is unchanged.
|
|
145
|
+
|
|
146
|
+
Two things to know:
|
|
147
|
+
|
|
148
|
+
- The model sees the placeholder, not the value. If it then calls a tool with the hidden value,
|
|
149
|
+
the tool gets the placeholder.
|
|
150
|
+
- If the hidden version can't be written back exactly, the call is not sent
|
|
151
|
+
(`NorcasterCannotCheck`). It is never sent with the original text.
|
|
152
|
+
|
|
153
|
+
## Answer checks
|
|
154
|
+
|
|
155
|
+
Off unless you turn them on. With `check_output=True` the middleware also asks Norcaster about each
|
|
156
|
+
of the model's answers, after every model call of the run and before the agent uses it
|
|
157
|
+
(`POST /v1/check/output`): its text, and the tool calls it asks for (each tool's name and its
|
|
158
|
+
arguments), so before the agent's tools node runs any of them:
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
NorcasterCheckMiddleware(norcaster, deployment_id="<the app's id>", check_output=True)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
| Norcaster's decision on the answer | What the agent gets |
|
|
165
|
+
|---|---|
|
|
166
|
+
| **allow** | The answer as the model wrote it. |
|
|
167
|
+
| **hide** | A copy with Norcaster's hidden text, and each tool call's arguments with the sensitive values hidden, in every place the message keeps them (`tool_calls`, the provider's content blocks, `additional_kwargs`). The tool runs with those arguments. The answer's token list (`logprobs`), which spells out the original words, is left out. |
|
|
168
|
+
| **block** | Nothing. The run stops with `NorcasterBlocked`, `about == "answer"`. A tool the answer asked for does not run. |
|
|
169
|
+
|
|
170
|
+
The answer's check is recorded under the same request as the check before the call, so the app's
|
|
171
|
+
records show one call with two decisions. Its receipt is on the answer, under
|
|
172
|
+
`response_metadata["norcaster"]["answer"]`. Each model call that answers with text or tool calls
|
|
173
|
+
adds one more round trip to Norcaster, before any tool runs.
|
|
174
|
+
|
|
175
|
+
**The app's rules must cover answers.** If they don't, Norcaster refuses the check:
|
|
176
|
+
`NorcasterCheckRefused` with code `output_rules_off` and `about == "answer"`. It happens after the
|
|
177
|
+
provider has answered, on every call, until the rules cover answers or `check_output` is off.
|
|
178
|
+
|
|
179
|
+
**What is read:** the answer's text, which is its string content or its text blocks run together,
|
|
180
|
+
and its tool calls: `tool_calls` (each tool's name and its `args` as compact JSON), the ones
|
|
181
|
+
LangChain couldn't parse (`invalid_tool_calls`, their arguments as the text the model wrote) and
|
|
182
|
+
OpenAI's older `function_call`. An answer that is only tool calls is asked about too.
|
|
183
|
+
|
|
184
|
+
**When a tool call is hidden:** the tool runs with placeholders in place of the sensitive values,
|
|
185
|
+
in JSON of the same shape (the same keys and value types; the tool's name never changes). A
|
|
186
|
+
placeholder can change what a tool does: a mail tool would get `[REDACTED: EMAIL]` as the address.
|
|
187
|
+
If a tool can't run with a placeholder, treat hide as block in that tool. A hide that can't be
|
|
188
|
+
written into every copy of the call withholds the answer (`NorcasterCannotCheck`).
|
|
189
|
+
|
|
190
|
+
**What is handed on unread:**
|
|
191
|
+
|
|
192
|
+
- **The model's reasoning** (thinking blocks). Providers sign it, so it is handed on as it is,
|
|
193
|
+
after a hide too: it can still hold what the hide took out of the text.
|
|
194
|
+
- **A refusal kept outside the text** (`additional_kwargs["refusal"]`), and usage.
|
|
195
|
+
- **Provider metadata** other than `logprobs`. Citations keep their offsets into the original text.
|
|
196
|
+
|
|
197
|
+
**What is withheld:** an answer that holds an image, a file, audio, or any part of a kind the
|
|
198
|
+
adapter doesn't know (results of tools the provider runs itself among them). It is never handed on
|
|
199
|
+
unread: `NorcasterCannotCheck`, `about == "answer"`. So is an answer that is hidden and was also
|
|
200
|
+
parsed into a structured response, from its text (`response_format` with the provider's structured
|
|
201
|
+
output) or from a tool call's arguments: the structured response holds the original words. Allowed,
|
|
202
|
+
it is handed on.
|
|
203
|
+
|
|
204
|
+
**The check engine in your own network** reads tool calls too. An older engine refuses them (409
|
|
205
|
+
`tool_calls_not_on_engine`), and the answer is withheld.
|
|
206
|
+
|
|
207
|
+
**An older `norcaster` SDK that can't ask about tool calls** (0.1.1 or older; this adapter requires
|
|
208
|
+
`norcaster>=0.1.2`, so only an SDK installed around that rule): an answer with tool calls is withheld
|
|
209
|
+
(`NorcasterCannotCheck`), never handed on with its tool calls unread. Answers with text only are
|
|
210
|
+
checked as before.
|
|
211
|
+
|
|
212
|
+
**What the record shows:** that the app asked about the answer and its tool calls, and which tools
|
|
213
|
+
(never their arguments). Not what the agent then did, or which tools ran.
|
|
214
|
+
|
|
215
|
+
**Streaming.** LangChain tells callbacks about the model's answer inside the model call, before
|
|
216
|
+
any middleware gets it back. So, with answer checks on:
|
|
217
|
+
|
|
218
|
+
- **`stream_mode="messages"`** (alone or with other modes, sync or async): the model call is tagged
|
|
219
|
+
`nostream`, and the stream carries each step's checked answer as one message at the end of the
|
|
220
|
+
step, not token by token. LangChain still streams from the provider; your app gets the first
|
|
221
|
+
words once the provider has finished and Norcaster has decided. `"updates"` and `"values"` work
|
|
222
|
+
as before.
|
|
223
|
+
- **Refused before anything is sent** (`NorcasterCannotCheck`): `astream_events`, `astream_log`,
|
|
224
|
+
and a callback of your own that takes the answer as it is written (`on_llm_new_token`), set on
|
|
225
|
+
the run or on the model.
|
|
226
|
+
- **Tracers (LangSmith) and other callbacks** are still told about the provider's answer as it
|
|
227
|
+
came when the model call ends (`on_llm_end`), as they are told about each step's messages before
|
|
228
|
+
a hide. Don't hand the answer on from a callback.
|
|
229
|
+
|
|
230
|
+
**When Norcaster can't be reached after the model answered,** the answer is withheld:
|
|
231
|
+
`NorcasterUnreachable` with `about == "answer"`. With `when_unreachable="let_through"` the agent
|
|
232
|
+
gets the answer, `response_metadata["norcaster"]["answer"]` says `"decision": "unchecked"`, and
|
|
233
|
+
Norcaster records it as an answer let through without a check, under the request it belongs to.
|
|
234
|
+
When the request itself was let through, its answer is not asked about: the one record stands for
|
|
235
|
+
the call.
|
|
236
|
+
|
|
237
|
+
**Retry and fallback middleware** placed before this one (`ModelRetryMiddleware`,
|
|
238
|
+
`ModelFallbackMiddleware`) retry or fall back on a block too, so the provider is called again. Each
|
|
239
|
+
try is checked and leaves its own records. The same holds for a blocked request.
|
|
240
|
+
|
|
241
|
+
## A graph you built yourself
|
|
242
|
+
|
|
243
|
+
The middleware works with `create_agent`. A graph built by hand with LangGraph, or a plain chain,
|
|
244
|
+
has no place for it: LangChain offers no hook around a model call there. Call the same check
|
|
245
|
+
yourself, right before the node calls the model:
|
|
246
|
+
|
|
247
|
+
```python
|
|
248
|
+
from norcaster_langchain import check_messages
|
|
249
|
+
|
|
250
|
+
def call_model(state):
|
|
251
|
+
checked = check_messages(norcaster, state["messages"], deployment_id="<the app's id>")
|
|
252
|
+
answer = model.invoke(checked.messages) # the messages to send: hidden when told to
|
|
253
|
+
return {"messages": [checked.stamp(answer)]} # the answer, with the record's id on it
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
`acheck_messages` is the same for async code. A block raises `NorcasterBlocked` before the model
|
|
257
|
+
is called.
|
|
258
|
+
|
|
259
|
+
**Here the check happens only where you call it.** A node that calls the model without it is not
|
|
260
|
+
checked and leaves no record. Call the model with the checked messages and no other content: an
|
|
261
|
+
`extra_body` or `model_kwargs` that holds messages, input, instructions, a prompt or a `prediction`
|
|
262
|
+
would reach the provider unchecked, and here nothing refuses it.
|
|
263
|
+
|
|
264
|
+
To ask about the answer too, call `check_answer` on it before the node returns it:
|
|
265
|
+
|
|
266
|
+
```python
|
|
267
|
+
from norcaster_langchain import check_answer, check_messages
|
|
268
|
+
|
|
269
|
+
def call_model(state):
|
|
270
|
+
checked = check_messages(norcaster, state["messages"], deployment_id="<the app's id>")
|
|
271
|
+
answer = model.invoke(checked.messages, config={"tags": ["nostream"]})
|
|
272
|
+
return {"messages": [check_answer(norcaster, answer, checked, deployment_id="<the app's id>")]}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
It returns the answer to use, with both receipts on it, and raises as the middleware does.
|
|
276
|
+
`acheck_answer` is the same for async code. The `nostream` tag keeps `stream_mode="messages"` from
|
|
277
|
+
sending the provider's answer before it is checked; the checked answer goes out when the node
|
|
278
|
+
returns. Without it, a streamed graph hands on the provider's tokens first.
|
|
279
|
+
|
|
280
|
+
## Settings
|
|
281
|
+
|
|
282
|
+
| Setting | Default | What it is |
|
|
283
|
+
|---|---|---|
|
|
284
|
+
| `client` | required | Your `NorcasterCloudClient`. It holds the key and your Norcaster address. |
|
|
285
|
+
| `deployment_id` | the key's app | The app's id. Leave it out only when the key belongs to one app. |
|
|
286
|
+
| `when_unreachable` | `"block"` | `"block"` or `"let_through"`. See below. |
|
|
287
|
+
| `on_unchecked` | none | Called every time a call is let through without a check. |
|
|
288
|
+
| `timeout` | `10` | Seconds to wait for Norcaster's answer. |
|
|
289
|
+
| `check_output` | `False` | Also ask about the text of each of the model's answers. See [Answer checks](#answer-checks). |
|
|
290
|
+
|
|
291
|
+
These are the Norcaster SDK's own settings for a check, passed on unchanged. A wrong setting is
|
|
292
|
+
refused when the middleware is built, so the app does not start with it.
|
|
293
|
+
|
|
294
|
+
**One middleware per app.** Each agent that is its own app in Norcaster gets its own
|
|
295
|
+
`deployment_id`, so each has its own rules and its own records.
|
|
296
|
+
|
|
297
|
+
## What your code gets
|
|
298
|
+
|
|
299
|
+
**The record's id, on every answer.** Each answer of the model carries the check that came before
|
|
300
|
+
the call that produced it:
|
|
301
|
+
|
|
302
|
+
```python
|
|
303
|
+
answer.response_metadata["norcaster"]
|
|
304
|
+
# {"decision": "hide", "receipt_id": "88bfb959-…", "content_sha256": "8cdc…", "rules_version": 3,
|
|
305
|
+
# "decided_at": "2026-11-12T16:07:41.512+00:00", "request_id": "chk_…"}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
With answer checks on, the answer's own check is under `"answer"`, with the same keys:
|
|
309
|
+
|
|
310
|
+
```python
|
|
311
|
+
answer.response_metadata["norcaster"]["answer"]
|
|
312
|
+
# {"decision": "allow", "receipt_id": "5d0c…", "content_sha256": "1f3a…", "rules_version": 3,
|
|
313
|
+
# "decided_at": "2026-11-12T16:07:42.104+00:00", "request_id": "chk_…"}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
It stays in the agent's messages and shows wherever you trace them. It never holds the
|
|
317
|
+
conversation, the answer or the key.
|
|
318
|
+
|
|
319
|
+
**An error when the call is not made, or the answer is not handed on.** All of them are
|
|
320
|
+
`NorcasterCheckError`, from the SDK. `about` says which: `"request"` (the provider was not called)
|
|
321
|
+
or `"answer"` (the provider answered and the answer was withheld).
|
|
322
|
+
|
|
323
|
+
| Error | When |
|
|
324
|
+
|---|---|
|
|
325
|
+
| `NorcasterBlocked` | The decision was block. It has `code`, `reason`, `suggestion` and `receipt_id`. |
|
|
326
|
+
| `NorcasterUnreachable` | Norcaster could not be reached and the call was not let through. |
|
|
327
|
+
| `NorcasterCheckRefused` | Norcaster refused the check: a wrong or revoked key, a missing scope, a stopped app, the rate limit, too much content, rules that don't cover answers (`output_rules_off`). |
|
|
328
|
+
| `NorcasterCannotCheck` | The call or the answer holds something a check can't read, the hidden version could not be written back, or the run is streamed in a way that would hand on the answer before it is checked. |
|
|
329
|
+
|
|
330
|
+
A block looks like this, and never repeats what was blocked:
|
|
331
|
+
|
|
332
|
+
```
|
|
333
|
+
NorcasterBlocked: Norcaster's decision was block (contextual_block). A secret or access credential
|
|
334
|
+
was detected in an earlier message in this conversation. The provider was not called.
|
|
335
|
+
Receipt: 8070b112-3270-45eb-ad0e-7cad5dbe55a0.
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
The error stops the agent's run. Catch it where you call the agent.
|
|
339
|
+
|
|
340
|
+
## When Norcaster can't be reached
|
|
341
|
+
|
|
342
|
+
"Can't be reached" means no connection, no answer within `timeout`, or an error on Norcaster's side.
|
|
343
|
+
|
|
344
|
+
**By default the call is blocked:** `NorcasterUnreachable`, and the provider is not called. With
|
|
345
|
+
answer checks on, an answer is withheld the same way ([Answer checks](#answer-checks)).
|
|
346
|
+
|
|
347
|
+
`when_unreachable="let_through"` makes the call instead. Every call that is let through is
|
|
348
|
+
recorded, each time:
|
|
349
|
+
|
|
350
|
+
1. A warning in your app's log (logger `norcaster.check`), with the call's id and its SHA-256.
|
|
351
|
+
2. Your `on_unchecked` function is called with it.
|
|
352
|
+
3. On the model's answer: `response_metadata["norcaster"]` says `"decision": "unchecked"` and
|
|
353
|
+
carries the id.
|
|
354
|
+
4. In Norcaster, once it answers again: the next check that succeeds reports what was let through,
|
|
355
|
+
and it shows in the app's records as **Let through without a check**.
|
|
356
|
+
|
|
357
|
+
The record holds a SHA-256 of the conversation, when it happened and why. It never holds the
|
|
358
|
+
conversation itself. No rule ran on a call that was let through, and it is not counted as a check.
|
|
359
|
+
|
|
360
|
+
Know these limits before you turn it on:
|
|
361
|
+
|
|
362
|
+
- A call that is let through goes to the provider as the agent wrote it. Nothing is hidden.
|
|
363
|
+
- Calls waiting to be reported are kept in your app's memory. If the app stops before Norcaster
|
|
364
|
+
answers again, your own log line is the only record. `norcaster.report_unchecked()` sends what
|
|
365
|
+
is waiting; call it when your app shuts down.
|
|
366
|
+
- At most 10,000 wait at a time. Past that, calls are blocked.
|
|
367
|
+
- A refusal from Norcaster always blocks, whatever the setting.
|
|
368
|
+
|
|
369
|
+
## Limits to know
|
|
370
|
+
|
|
371
|
+
- **Each model call is one check.** An agent run with five model calls is five checks. A check
|
|
372
|
+
draws on the same rate limit as a request through Norcaster's gateway: 600 a minute by default,
|
|
373
|
+
shared by all of one person's keys, or by one service account's or agent's keys. Over the limit,
|
|
374
|
+
Norcaster refuses and the call is blocked. Like gatewayed requests, checks don't count toward the
|
|
375
|
+
plan's monthly request limit yet.
|
|
376
|
+
- **Each model call waits for Norcaster's answer.** Measured against a local Norcaster: about
|
|
377
|
+
11 ms for a short prompt and about 19 ms for a 4 KB conversation, at the median. A hosted
|
|
378
|
+
Norcaster adds the network round trip.
|
|
379
|
+
- **Each check sends the whole conversation so far**, so the wait grows as the conversation does.
|
|
380
|
+
More than 1,000,000 characters is refused.
|
|
381
|
+
- **Answer checks read text only**, and add one round trip per model call that answers with text.
|
|
382
|
+
A streamed answer reaches your app once the provider has finished and Norcaster has decided.
|
|
383
|
+
See [Answer checks](#answer-checks).
|
|
384
|
+
- **Async runs: one Norcaster client per event loop.** The SDK keeps one connection open for async
|
|
385
|
+
checks, and it belongs to the event loop that first used it.
|
|
386
|
+
|
|
387
|
+
## Develop
|
|
388
|
+
|
|
389
|
+
The tests need LangChain, LangChain's OpenAI package and the SDK from this repository
|
|
390
|
+
(`sdk/python`). From this folder:
|
|
391
|
+
|
|
392
|
+
```bash
|
|
393
|
+
PYTHONPATH=src:../../sdk/python/src uv run --isolated --no-project --python 3.12 \
|
|
394
|
+
--with "langchain==1.5.0" --with "langchain-openai>=1.0.0,<2" --with "httpx>=0.24" --with "pytest>=7" \
|
|
395
|
+
python -m pytest -q
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
- `test/test_conversation.py`: what is sent to Norcaster for each kind of message, and how a hidden
|
|
399
|
+
version is written back.
|
|
400
|
+
- `test/test_agent.py`: the middleware inside a real agent, with a stand-in model.
|
|
401
|
+
- `test/test_answer.py`: answer checks, in an agent, in a streamed run, and by hand.
|
|
402
|
+
- `test/test_openai_wire.py`: the same on LangChain's real OpenAI chat model, against a stand-in
|
|
403
|
+
provider on `127.0.0.1`, to show what goes over the wire.
|
|
404
|
+
- `test/test_example.py`: the example agent.
|
|
405
|
+
|
|
406
|
+
Norcaster is a stand-in on `127.0.0.1` in all of them. No provider and no Norcaster API is called.
|