nace-haystack 0.1.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.
Files changed (41) hide show
  1. nace_haystack-0.1.0/.gitignore +15 -0
  2. nace_haystack-0.1.0/CHANGELOG.md +51 -0
  3. nace_haystack-0.1.0/LICENSE.txt +201 -0
  4. nace_haystack-0.1.0/PKG-INFO +230 -0
  5. nace_haystack-0.1.0/README.md +202 -0
  6. nace_haystack-0.1.0/pyproject.toml +182 -0
  7. nace_haystack-0.1.0/src/haystack_integrations/common/nace/__init__.py +3 -0
  8. nace_haystack-0.1.0/src/haystack_integrations/common/nace/client.py +171 -0
  9. nace_haystack-0.1.0/src/haystack_integrations/common/nace/concurrency.py +49 -0
  10. nace_haystack-0.1.0/src/haystack_integrations/common/nace/errors.py +138 -0
  11. nace_haystack-0.1.0/src/haystack_integrations/common/nace/images.py +204 -0
  12. nace_haystack-0.1.0/src/haystack_integrations/common/nace/request.py +144 -0
  13. nace_haystack-0.1.0/src/haystack_integrations/common/py.typed +0 -0
  14. nace_haystack-0.1.0/src/haystack_integrations/components/classifiers/nace/__init__.py +7 -0
  15. nace_haystack-0.1.0/src/haystack_integrations/components/classifiers/nace/document_classifier.py +390 -0
  16. nace_haystack-0.1.0/src/haystack_integrations/components/classifiers/py.typed +0 -0
  17. nace_haystack-0.1.0/src/haystack_integrations/components/evaluators/nace/__init__.py +13 -0
  18. nace_haystack-0.1.0/src/haystack_integrations/components/evaluators/nace/_base.py +247 -0
  19. nace_haystack-0.1.0/src/haystack_integrations/components/evaluators/nace/context_relevance.py +139 -0
  20. nace_haystack-0.1.0/src/haystack_integrations/components/evaluators/nace/faithfulness.py +169 -0
  21. nace_haystack-0.1.0/src/haystack_integrations/components/evaluators/py.typed +0 -0
  22. nace_haystack-0.1.0/src/haystack_integrations/components/routers/nace/__init__.py +7 -0
  23. nace_haystack-0.1.0/src/haystack_integrations/components/routers/nace/text_router.py +316 -0
  24. nace_haystack-0.1.0/src/haystack_integrations/components/routers/py.typed +0 -0
  25. nace_haystack-0.1.0/tests/__init__.py +3 -0
  26. nace_haystack-0.1.0/tests/conftest.py +20 -0
  27. nace_haystack-0.1.0/tests/fixtures/nace_pipeline.yaml +56 -0
  28. nace_haystack-0.1.0/tests/fixtures/typesafe_pipeline.yaml +56 -0
  29. nace_haystack-0.1.0/tests/helpers.py +72 -0
  30. nace_haystack-0.1.0/tests/integration/__init__.py +3 -0
  31. nace_haystack-0.1.0/tests/integration/test_live.py +220 -0
  32. nace_haystack-0.1.0/tests/test_client.py +153 -0
  33. nace_haystack-0.1.0/tests/test_concurrency.py +50 -0
  34. nace_haystack-0.1.0/tests/test_document_classifier.py +494 -0
  35. nace_haystack-0.1.0/tests/test_errors.py +159 -0
  36. nace_haystack-0.1.0/tests/test_evaluators.py +302 -0
  37. nace_haystack-0.1.0/tests/test_images.py +167 -0
  38. nace_haystack-0.1.0/tests/test_packaging.py +44 -0
  39. nace_haystack-0.1.0/tests/test_request.py +177 -0
  40. nace_haystack-0.1.0/tests/test_switching.py +140 -0
  41. nace_haystack-0.1.0/tests/test_text_router.py +372 -0
@@ -0,0 +1,15 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ dist/
5
+ build/
6
+ *.egg-info/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ coverage.xml
12
+ .env
13
+ .DS_Store
14
+ .ipynb_checkpoints/
15
+ examples/data/scanned_invoice.png
@@ -0,0 +1,51 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ## [0.1.0] - 2026-10-29
6
+
7
+ First release.
8
+
9
+ ### Added
10
+
11
+ - `NaceTextRouter`: routes a text to one output per label with Drex, with a `low_confidence` fallback and an opt-in
12
+ `probabilities` output. Takes images (`ImageContent`, `ByteStream`, or files uploaded to Drex).
13
+ - `NaceDocumentClassifier`: answers typed questions about each document and stores Drex's answers in its metadata,
14
+ with parallel requests and per-document failure handling.
15
+ - `NaceFaithfulnessEvaluator` and `NaceContextRelevanceEvaluator`: the inputs and outputs of Haystack's built-in LLM
16
+ evaluators, with one Drex request per item.
17
+ - Same parameters and outputs as deepset's `typesafe-haystack` components, so Jev pipelines switch with the documented
18
+ edits.
19
+ - Default model `drex-max-v1.1` (Drex Max v1.1). Any model ID is sent as given, and answers keep every field Drex returns, so new Drex
20
+ models, question types and answer fields work without a new release.
21
+ - Images too big for one request are shrunk to fit (Pillow, optional).
22
+ - OpenRouter backend, attribution headers, a 60-second timeout, and retries on 429 and 529 that honour
23
+ `retry-after-ms`.
24
+ - Readable errors that keep nace-sdk's error classes.
25
+
26
+ ### Changed
27
+
28
+ - Licensed under Apache-2.0, the licence of Haystack's own integrations.
29
+
30
+ ### Fixed
31
+
32
+ Found while preparing the release, before anything was published:
33
+
34
+ - With `raise_on_failure=True`, the classifier and evaluators stop sending requests as soon as one fails, instead of
35
+ sending every queued request first.
36
+ - Waits that the server asks for between retries are capped at 60 seconds.
37
+ - OpenRouter variants of text-only models, such as `nace-ai/drex-v2.0:floor`, are refused images before sending,
38
+ like the models themselves.
39
+ - A `ByteStream` typed `application/octet-stream` is sent with its real image type.
40
+ - A 402 from OpenRouter points to OpenRouter's credits page instead of Nace billing.
41
+ - HTML error pages are summarised in one line, and long error messages are cut to 500 characters.
42
+ - `run_async` works again after the event loop changes, for example a second `asyncio.run` or notebook cell; it
43
+ failed with "Event loop is closed".
44
+ - A 16-bit grayscale scan that has to be shrunk keeps its content instead of turning into a blank white page.
45
+ - A 404 or 405 error names the URL it went to, so a wrong base URL is easy to spot.
46
+ - The router raises a clear error when the backend answers with a label it wasn't given, instead of dropping the
47
+ text.
48
+ - A label named `type` with a description is refused, because a saved pipeline with it couldn't be loaded.
49
+ - The evaluators explain that `images` takes one list per item when given a flat list.
50
+ - `pydantic>=2.0` and `filetype>=1.0.0` are declared as dependencies; `filetype` 0.x can't be imported on
51
+ Python 3.11.
@@ -0,0 +1,201 @@
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
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the 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,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026-present Nace AI
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,230 @@
1
+ Metadata-Version: 2.5
2
+ Name: nace-haystack
3
+ Version: 0.1.0
4
+ Summary: Haystack integration for Drex decision models by Nace
5
+ Project-URL: Documentation, https://console.nace.ai/docs
6
+ Project-URL: Issues, https://github.com/nace-ai/nace-haystack/issues
7
+ Project-URL: Source, https://github.com/nace-ai/nace-haystack
8
+ Author-email: Nace AI <hello@nace.ai>
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE.txt
11
+ Keywords: Drex,Haystack,Nace,classification,decision model,evaluation,routing
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Programming Language :: Python :: 3.15
20
+ Classifier: Programming Language :: Python :: Implementation :: CPython
21
+ Requires-Python: >=3.11
22
+ Requires-Dist: filetype>=1.0.0
23
+ Requires-Dist: haystack-ai>=3.0.0
24
+ Requires-Dist: httpx<1.0,>=0.27
25
+ Requires-Dist: nace-sdk<1.0.0,>=0.3.0
26
+ Requires-Dist: pydantic>=2.0
27
+ Description-Content-Type: text/markdown
28
+
29
+ # nace-haystack
30
+
31
+ Haystack components for [Drex](https://console.nace.ai/docs), Nace's decision models.
32
+
33
+ Drex models don't generate text. They answer typed questions about a text, JSON or images in one request and return
34
+ calibrated probabilities:
35
+
36
+ - `choice` picks one label from a set of options,
37
+ - `score` rates the input on ordered levels,
38
+ - `noul` gives the probability that a yes/no statement is true.
39
+
40
+ This package brings them to Haystack pipelines:
41
+
42
+ | Component | What it does |
43
+ | --- | --- |
44
+ | `NaceTextRouter` | Routes a text (and optional images) to one output per label, with a `low_confidence` fallback |
45
+ | `NaceDocumentClassifier` | Answers questions about each document and stores the answers in its metadata |
46
+ | `NaceFaithfulnessEvaluator` | Judges whether answers are supported by their contexts, like Haystack's `FaithfulnessEvaluator` |
47
+ | `NaceContextRelevanceEvaluator` | Judges whether retrieved contexts help answer the question, like `ContextRelevanceEvaluator` |
48
+
49
+ The router and classifier mirror deepset's `typesafe-haystack` components for Jev, so a Jev pipeline switches to Drex
50
+ with a few edits (see [Switching from TypeSafe](#switching-from-typesafe)).
51
+
52
+ ## Installation
53
+
54
+ ```bash
55
+ pip install nace-haystack
56
+ ```
57
+
58
+ Create an API key at [console.nace.ai](https://console.nace.ai) and set it as `NACE_API_KEY`.
59
+
60
+ ## Usage
61
+
62
+ ### Route text
63
+
64
+ ```python
65
+ from haystack_integrations.components.routers.nace import NaceTextRouter
66
+
67
+ router = NaceTextRouter(
68
+ labels={
69
+ "billing": "Payments, invoices, refunds and charges",
70
+ "technical": "Bugs, crashes and errors in the product",
71
+ },
72
+ instructions="Which team should handle this support request?",
73
+ min_confidence=0.6,
74
+ )
75
+
76
+ print(router.run(text="I was charged twice for my subscription."))
77
+ # {'billing': 'I was charged twice for my subscription.'}
78
+ ```
79
+
80
+ Texts whose answer is less confident than `min_confidence` go to the `low_confidence` output, for a fallback such
81
+ as an LLM or a person. With `include_probabilities=True`, a `probabilities` output carries the full answer, so
82
+ thresholds stay in your pipeline.
83
+
84
+ ### Classify documents
85
+
86
+ ```python
87
+ from haystack import Document
88
+
89
+ from haystack_integrations.components.classifiers.nace import NaceDocumentClassifier
90
+
91
+ classifier = NaceDocumentClassifier(
92
+ questions={
93
+ "department": {
94
+ "type": "choice",
95
+ "instructions": "Which department should handle this ticket?",
96
+ "criteria": {"billing": None, "technical": None, "sales": None},
97
+ },
98
+ "urgency": {
99
+ "type": "score",
100
+ "instructions": "How urgent is this ticket?",
101
+ "criteria": ["can wait", "this week", "today"],
102
+ },
103
+ "refund": {"type": "noul", "instructions": "Is the customer asking for a refund?"},
104
+ },
105
+ )
106
+
107
+ result = classifier.run(documents=[Document(content="I was charged twice, please refund me today.")])
108
+ print(result["documents"][0].meta["nace"]["department"])
109
+ # {'type': 'choice', 'choice': 'billing', 'confidence': 0.97, 'probabilities': {...}}
110
+ ```
111
+
112
+ Each document is one request carrying every question, with up to `max_workers` requests (default 3) at once.
113
+ Documents that can't be classified are returned in `failed_documents` with the reason in
114
+ `meta["classification_error"]`.
115
+
116
+ ### Evaluate RAG answers
117
+
118
+ ```python
119
+ from haystack_integrations.components.evaluators.nace import NaceFaithfulnessEvaluator
120
+
121
+ evaluator = NaceFaithfulnessEvaluator()
122
+ result = evaluator.run(
123
+ questions=["Who won the race?"],
124
+ contexts=[["Ada won the 2026 city race."]],
125
+ predicted_answers=["Ada won the race."],
126
+ )
127
+ print(result["score"], result["individual_scores"])
128
+ ```
129
+
130
+ Both evaluators take the same inputs and return the same outputs (`score`, `individual_scores`, `results`, `meta`) as
131
+ Haystack's built-in LLM evaluators. Each item is one Drex request, and its score is the probability that the answer
132
+ is faithful (or that the contexts are relevant).
133
+
134
+ ## Models
135
+
136
+ The default model is `drex-max-v1.1` (Drex Max v1.1), which reads text, JSON and images. The default moves to the
137
+ newest Drex model in package releases, and a new model can shift probabilities, so **pin `model=` in production** if
138
+ your pipeline relies on thresholds.
139
+
140
+ `drex-v2.0` reads text only and bills fewer tokens on short texts: for a short email with three questions, we
141
+ measured 74 input tokens on `drex-v2.0` and 165 on `drex-max-v1.1` (2026-10-29). See
142
+ [Drex's limits](https://console.nace.ai/docs/drex/reference/limits) for questions per request and state sizes.
143
+ Any model ID is sent as given, so new Drex models work without a new release of this package.
144
+
145
+ ## Images
146
+
147
+ The router and the evaluators take images: `ImageContent`, `ByteStream`, or files uploaded to Drex with nace-sdk
148
+ (`client.documents.upload(...)`). Prompts refer to them as "Image 1", "Image 2" and so on.
149
+
150
+ ```python
151
+ from haystack.dataclasses import ImageContent
152
+
153
+ router = NaceTextRouter(
154
+ labels={"invoice": "A bill or invoice", "receipt": "A proof of payment", "other": None},
155
+ instructions="What kind of document is Image 1?",
156
+ )
157
+ router.run(text="A customer attached a scan.", images=[ImageContent.from_file_path("scan.jpg")])
158
+ ```
159
+
160
+ Images too big to send in one request are shrunk automatically, with a logged warning. Drex Max reads images at about
161
+ 1.6 megapixels (1,638,400 pixels), and an image is never shrunk below about 1 megapixel: if the text leaves too little
162
+ room for the images, you get a clear error instead of a blurry image. Photos are turned upright using their EXIF
163
+ orientation before shrinking. Shrinking needs `pip install pillow`; to skip it, downscale earlier with
164
+ `ImageFileToImageContent(size=(2048, 2048))` or `PDFToImageContent(size=(2048, 2048))`. PDFs must be converted to
165
+ images first.
166
+
167
+ ## Other backends
168
+
169
+ Set `api_base_url` to use Drex through [OpenRouter](https://openrouter.ai) (`https://openrouter.ai/api/v1`, with
170
+ model IDs such as `nace-ai/drex-max`) or another TypeSafe-compatible host. OpenRouter takes inline images only.
171
+
172
+ ## Errors and retries
173
+
174
+ Requests that hit a rate limit (429) or an overloaded model (529) are retried, waiting as long as Drex asks. Other
175
+ errors are raised as nace-sdk's error classes with a readable message, for example:
176
+
177
+ ```text
178
+ 422 from Drex: the request is invalid
179
+ questions.department.criteria: must have at least 2 labels
180
+ ```
181
+
182
+ ## Switching from TypeSafe
183
+
184
+ The router and classifier take the same parameters and produce the same outputs as `TypeSafeTextRouter` and
185
+ `TypeSafeDocumentClassifier`.
186
+
187
+ In Python code that uses Jev's default model, change the import, the class name and the API key:
188
+
189
+ ```diff
190
+ -from haystack_integrations.components.routers.typesafe import TypeSafeTextRouter
191
+ +from haystack_integrations.components.routers.nace import NaceTextRouter
192
+
193
+ -router = TypeSafeTextRouter(labels=..., min_confidence=0.6)
194
+ +router = NaceTextRouter(labels=..., min_confidence=0.6)
195
+ ```
196
+
197
+ ```diff
198
+ -export TYPESAFE_API_KEY=...
199
+ +export NACE_API_KEY=...
200
+ ```
201
+
202
+ The classifier writes answers to `meta["nace"]` by default. If your code reads `meta["typesafe"]` (in a filter or a
203
+ `MetadataRouter` rule, for example), pass `metadata_field="typesafe"` to keep the old key.
204
+
205
+ Questions built with `typesafe_sdk` objects (`Choice`, `Score`, `Noul`) work as they are; you can switch them to
206
+ `nace_sdk`'s classes of the same names when convenient. Where your code catches `TypeSafeError`, catch
207
+ `nace_sdk.NaceError` instead.
208
+
209
+ In a saved YAML pipeline, make three edits per component:
210
+
211
+ ```diff
212
+ - type: haystack_integrations.components.routers.typesafe.text_router.TypeSafeTextRouter
213
+ + type: haystack_integrations.components.routers.nace.text_router.NaceTextRouter
214
+ ...
215
+ - - TYPESAFE_API_KEY
216
+ + - NACE_API_KEY
217
+ ...
218
+ - model: jev-latest
219
+ + model: drex-max-v1.1
220
+ ```
221
+
222
+ The classifier's type becomes `haystack_integrations.components.classifiers.nace.document_classifier.NaceDocumentClassifier`.
223
+ A saved pipeline keeps `metadata_field: typesafe`, so answers stay where your filters read them. Model names aren't
224
+ translated: Drex rejects `jev-latest` with a message listing its models.
225
+
226
+ Drex's own guide covers the API side: [Migrate from TypeSafe](https://console.nace.ai/docs/drex/guides/migrate-from-typesafe).
227
+
228
+ ## License
229
+
230
+ Apache-2.0