courier-encode 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,1158 @@
1
+ Metadata-Version: 2.4
2
+ Name: courier-encode
3
+ Version: 0.1.0
4
+ Summary: Python SDK for OpenAI-compatible inference endpoints (Courier and friends) with auto tool-call loops, structured outputs, and Whisper.
5
+ Project-URL: Homepage, https://getcourier.ai
6
+ Project-URL: Documentation, https://getcourier.ai/docs
7
+ Project-URL: Repository, https://github.com/recursion-ai/encode
8
+ Author: Recursion AI
9
+ License: Apache License
10
+ Version 2.0, January 2004
11
+ http://www.apache.org/licenses/
12
+
13
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
14
+
15
+ 1. Definitions.
16
+
17
+ "License" shall mean the terms and conditions for use, reproduction,
18
+ and distribution as defined by Sections 1 through 9 of this document.
19
+
20
+ "Licensor" shall mean the copyright owner or entity authorized by
21
+ the copyright owner that is granting the License.
22
+
23
+ "Legal Entity" shall mean the union of the acting entity and all
24
+ other entities that control, are controlled by, or are under common
25
+ control with that entity. For the purposes of this definition,
26
+ "control" means (i) the power, direct or indirect, to cause the
27
+ direction or management of such entity, whether by contract or
28
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
29
+ outstanding shares, or (iii) beneficial ownership of such entity.
30
+
31
+ "You" (or "Your") shall mean an individual or Legal Entity
32
+ exercising permissions granted by this License.
33
+
34
+ "Source" form shall mean the preferred form for making modifications,
35
+ including but not limited to software source code, documentation
36
+ source, and configuration files.
37
+
38
+ "Object" form shall mean any form resulting from mechanical
39
+ transformation or translation of a Source form, including but
40
+ not limited to compiled object code, generated documentation,
41
+ and conversions to other media types.
42
+
43
+ "Work" shall mean the work of authorship, whether in Source or
44
+ Object form, made available under the License, as indicated by a
45
+ copyright notice that is included in or attached to the work
46
+ (an example is provided in the Appendix below).
47
+
48
+ "Derivative Works" shall mean any work, whether in Source or Object
49
+ form, that is based on (or derived from) the Work and for which the
50
+ editorial revisions, annotations, elaborations, or other modifications
51
+ represent, as a whole, an original work of authorship. For the purposes
52
+ of this License, Derivative Works shall not include works that remain
53
+ separable from, or merely link (or bind by name) to the interfaces of,
54
+ the Work and Derivative Works thereof.
55
+
56
+ "Contribution" shall mean any work of authorship, including
57
+ the original version of the Work and any modifications or additions
58
+ to that Work or Derivative Works thereof, that is intentionally
59
+ submitted to Licensor for inclusion in the Work by the copyright owner
60
+ or by an individual or Legal Entity authorized to submit on behalf of
61
+ the copyright owner. For the purposes of this definition, "submitted"
62
+ means any form of electronic, verbal, or written communication sent
63
+ to the Licensor or its representatives, including but not limited to
64
+ communication on electronic mailing lists, source code control systems,
65
+ and issue tracking systems that are managed by, or on behalf of, the
66
+ Licensor for the purpose of discussing and improving the Work, but
67
+ excluding communication that is conspicuously marked or otherwise
68
+ designated in writing by the copyright owner as "Not a Contribution."
69
+
70
+ "Contributor" shall mean Licensor and any individual or Legal Entity
71
+ on behalf of whom a Contribution has been received by Licensor and
72
+ subsequently incorporated within the Work.
73
+
74
+ 2. Grant of Copyright 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
+ copyright license to reproduce, prepare Derivative Works of,
78
+ publicly display, publicly perform, sublicense, and distribute the
79
+ Work and such Derivative Works in Source or Object form.
80
+
81
+ 3. Grant of Patent License. Subject to the terms and conditions of
82
+ this License, each Contributor hereby grants to You a perpetual,
83
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
84
+ (except as stated in this section) patent license to make, have made,
85
+ use, offer to sell, sell, import, and otherwise transfer the Work,
86
+ where such license applies only to those patent claims licensable
87
+ by such Contributor that are necessarily infringed by their
88
+ Contribution(s) alone or by combination of their Contribution(s)
89
+ with the Work to which such Contribution(s) was submitted. If You
90
+ institute patent litigation against any entity (including a
91
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
92
+ or a Contribution incorporated within the Work constitutes direct
93
+ or contributory patent infringement, then any patent licenses
94
+ granted to You under this License for that Work shall terminate
95
+ as of the date such litigation is filed.
96
+
97
+ 4. Redistribution. You may reproduce and distribute copies of the
98
+ Work or Derivative Works thereof in any medium, with or without
99
+ modifications, and in Source or Object form, provided that You
100
+ meet the following conditions:
101
+
102
+ (a) You must give any other recipients of the Work or
103
+ Derivative Works a copy of this License; and
104
+
105
+ (b) You must cause any modified files to carry prominent notices
106
+ stating that You changed the files; and
107
+
108
+ (c) You must retain, in the Source form of any Derivative Works
109
+ that You distribute, all copyright, patent, trademark, and
110
+ attribution notices from the Source form of the Work,
111
+ excluding those notices that do not pertain to any part of
112
+ the Derivative Works; and
113
+
114
+ (d) If the Work includes a "NOTICE" text file as part of its
115
+ distribution, then any Derivative Works that You distribute must
116
+ include a readable copy of the attribution notices contained
117
+ within such NOTICE file, excluding those notices that do not
118
+ pertain to any part of the Derivative Works, in at least one
119
+ of the following places: within a NOTICE text file distributed
120
+ as part of the Derivative Works; within the Source form or
121
+ documentation, if provided along with the Derivative Works; or,
122
+ within a display generated by the Derivative Works, if and
123
+ wherever such third-party notices normally appear. The contents
124
+ of the NOTICE file are for informational purposes only and
125
+ do not modify the License. You may add Your own attribution
126
+ notices within Derivative Works that You distribute, alongside
127
+ or as an addendum to the NOTICE text from the Work, provided
128
+ that such additional attribution notices cannot be construed
129
+ as modifying the License.
130
+
131
+ You may add Your own copyright statement to Your modifications and
132
+ may provide additional or different license terms and conditions
133
+ for use, reproduction, or distribution of Your modifications, or
134
+ for any such Derivative Works as a whole, provided Your use,
135
+ reproduction, and distribution of the Work otherwise complies with
136
+ the conditions stated in this License.
137
+
138
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
139
+ any Contribution intentionally submitted for inclusion in the Work
140
+ by You to the Licensor shall be under the terms and conditions of
141
+ this License, without any additional terms or conditions.
142
+ Notwithstanding the above, nothing herein shall supersede or modify
143
+ the terms of any separate license agreement you may have executed
144
+ with Licensor regarding such Contributions.
145
+
146
+ 6. Trademarks. This License does not grant permission to use the trade
147
+ names, trademarks, service marks, or product names of the Licensor,
148
+ except as required for reasonable and customary use in describing the
149
+ origin of the Work and reproducing the content of the NOTICE file.
150
+
151
+ 7. Disclaimer of Warranty. Unless required by applicable law or
152
+ agreed to in writing, Licensor provides the Work (and each
153
+ Contributor provides its Contributions) on an "AS IS" BASIS,
154
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
155
+ implied, including, without limitation, any warranties or conditions
156
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
157
+ PARTICULAR PURPOSE. You are solely responsible for determining the
158
+ appropriateness of using or redistributing the Work and assume any
159
+ risks associated with Your exercise of permissions under this License.
160
+
161
+ 8. Limitation of Liability. In no event and under no legal theory,
162
+ whether in tort (including negligence), contract, or otherwise,
163
+ unless required by applicable law (such as deliberate and grossly
164
+ negligent acts) or agreed to in writing, shall any Contributor be
165
+ liable to You for damages, including any direct, indirect, special,
166
+ incidental, or consequential damages of any character arising as a
167
+ result of this License or out of the use or inability to use the
168
+ Work (including but not limited to damages for loss of goodwill,
169
+ work stoppage, computer failure or malfunction, or any and all
170
+ other commercial damages or losses), even if such Contributor
171
+ has been advised of the possibility of such damages.
172
+
173
+ 9. Accepting Warranty or Additional Liability. While redistributing
174
+ the Work or Derivative Works thereof, You may choose to offer,
175
+ and charge a fee for, acceptance of support, warranty, indemnity,
176
+ or other liability obligations and/or rights consistent with this
177
+ License. However, in accepting such obligations, You may act only
178
+ on Your own behalf and on Your sole responsibility, not on behalf
179
+ of any other Contributor, and only if You agree to indemnify,
180
+ defend, and hold each Contributor harmless for any liability
181
+ incurred by, or claims asserted against, such Contributor by reason
182
+ of your accepting any such warranty or additional liability.
183
+
184
+ END OF TERMS AND CONDITIONS
185
+
186
+ APPENDIX: How to apply the Apache License to your work.
187
+
188
+ To apply the Apache License to your work, attach the following
189
+ boilerplate notice, with the fields enclosed by brackets "[]"
190
+ replaced with your own identifying information. (Don't include
191
+ the brackets!) The text should be enclosed in the appropriate
192
+ comment syntax for the file format. We also recommend that a
193
+ file or class name and description of purpose be included on the
194
+ same "printed page" as the copyright notice for easier
195
+ identification within third-party archives.
196
+
197
+ Copyright [yyyy] [name of copyright owner]
198
+
199
+ Licensed under the Apache License, Version 2.0 (the "License");
200
+ you may not use this file except in compliance with the License.
201
+ You may obtain a copy of the License at
202
+
203
+ http://www.apache.org/licenses/LICENSE-2.0
204
+
205
+ Unless required by applicable law or agreed to in writing, software
206
+ distributed under the License is distributed on an "AS IS" BASIS,
207
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
208
+ See the License for the specific language governing permissions and
209
+ limitations under the License.
210
+ License-File: LICENSE
211
+ Keywords: agents,courier,llm,openai,sdk,tool-calling,whisper
212
+ Classifier: Development Status :: 3 - Alpha
213
+ Classifier: Intended Audience :: Developers
214
+ Classifier: License :: OSI Approved :: MIT License
215
+ Classifier: Programming Language :: Python :: 3
216
+ Classifier: Programming Language :: Python :: 3.10
217
+ Classifier: Programming Language :: Python :: 3.11
218
+ Classifier: Programming Language :: Python :: 3.12
219
+ Classifier: Programming Language :: Python :: 3.13
220
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
221
+ Classifier: Typing :: Typed
222
+ Requires-Python: >=3.10
223
+ Requires-Dist: httpx>=0.27
224
+ Requires-Dist: pexpect>=4.9
225
+ Requires-Dist: pydantic>=2.7
226
+ Requires-Dist: python-dotenv>=1.0
227
+ Requires-Dist: typing-extensions>=4.10
228
+ Provides-Extra: dev
229
+ Requires-Dist: build>=1.2; extra == 'dev'
230
+ Requires-Dist: mypy>=1.10; extra == 'dev'
231
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
232
+ Requires-Dist: pytest>=8.0; extra == 'dev'
233
+ Requires-Dist: respx>=0.21; extra == 'dev'
234
+ Requires-Dist: ruff>=0.5; extra == 'dev'
235
+ Description-Content-Type: text/markdown
236
+
237
+ # Courier — ENCODE
238
+
239
+ Courier — ENCODE is an SDK specifically designed for developing AI apps and agents with open source
240
+ LLMs.
241
+
242
+ ## Features
243
+
244
+ - Python SDK
245
+ - Request wrapper that sends messages to v1/chat/completions and v1/responses, configurable for each, auto formatting,
246
+ and returns a pydantic response with props the end user can use in their code.
247
+ - The request wrapper includes agentic processing with an optional 'tools' prop. Python functions can be passed and
248
+ automatically converted into tools for an agent. The SDK should parse out tool responses and call the functions,
249
+ and automatically send a follow up message to the agent. The SDK should be considered an option that runs until
250
+ it's completed it's tool calling, and then return a full list of messages in openai format.
251
+ - The request wrapper should optionally accept Pydantic models as a response_format for structured JSON responses
252
+ and automatically apply the format. The response object should be auto formatted to that pydantic model and usable
253
+ as such.
254
+ - The request wrapper should accept a pydantic model of messages. Images and audio should be included as well.
255
+ - a model name should be able to be selected.
256
+ - A whisper request wrapper that allows for transcriptions and translations
257
+
258
+ ## Notes
259
+
260
+ **encode should be a `uv` project and structured to be deployed on PyPi as an open source pip package.**
261
+
262
+ Everywhere that pydantic is accepted JSON should be accepted as well.
263
+
264
+ The tool call loop runs until the model doesn't make a tool call (or a hard capped limit that can be specified). So, if
265
+ a tool call is present, the code should execute and the response appended to the messages, and then another API requests
266
+ sent to the model. Once a response comes in with no acions the code continues past the relay loop.
267
+
268
+ Web search should be a boolean to enable. If enabled it should automatically be appended using the shorthand schema.
269
+
270
+ ### function names
271
+
272
+ - `encode.relay()` — the chat completions and responses wrapper
273
+ - `.intercept() ` — listener that can be attached to a `relay()` call and execute code whenever a tool call loop is
274
+ engaged, where the model is processing requests until the loop ends. Intercept runs every time a tool call
275
+ finishes even if the model is continuing the loop.
276
+ - `encode.whisper()` — whisper function. Accepts audio and can translate or transcribe.
277
+
278
+ ### Courier Docs
279
+
280
+ [Courier](https://getcourier.ai/docs)
281
+
282
+ #### API docs
283
+
284
+ ## API Docs
285
+
286
+ ### Courier Inference API
287
+
288
+ Courier provides a custom inference API optimized for n8n and other workflows.
289
+
290
+ #### POST /inference/
291
+
292
+ ```json
293
+ {
294
+ "model_name": "Solar Open 100B",
295
+ "model_id": "Model_UUID",
296
+ "model_type": "text-text",
297
+ "messages": [
298
+ {
299
+ "role": "system",
300
+ "content": "You are a helpful assistant"
301
+ },
302
+ {
303
+ "role": "user",
304
+ "content": "hello"
305
+ }
306
+ ],
307
+ "temperature": 0.7
308
+ }
309
+ ```
310
+
311
+ #### Authentication
312
+
313
+ For the Courier **/inference/** endpoint, use token authentication:
314
+
315
+ ```
316
+ Authorization: API_KEY
317
+ ```
318
+
319
+ ### OpenAI Compatible Endpoints
320
+
321
+ Courier supports OpenAI-compatible APIs for completions and responses workflows.
322
+
323
+ #### POST /v1/chat/completions
324
+
325
+ ```json
326
+ {
327
+ "model": "Solar Open 100B",
328
+ "messages": [
329
+ {
330
+ "role": "system",
331
+ "content": "You are a helpful assistant"
332
+ },
333
+ {
334
+ "role": "user",
335
+ "content": "hello"
336
+ }
337
+ ],
338
+ "temperature": 0.7
339
+ }
340
+ ```
341
+
342
+ #### GET /v1/models
343
+
344
+ ```json
345
+ {
346
+ "object": "list",
347
+ "data": [
348
+ {
349
+ "id": "model_name",
350
+ "object": "model",
351
+ "created": 1686935092,
352
+ "owned_by": "recursion-ai"
353
+ }
354
+ ]
355
+ }
356
+ ```
357
+
358
+ #### POST /v1/responses
359
+
360
+ ```json
361
+ {
362
+ "model": "my-shared-model",
363
+ "input": "Summarize this in one sentence.",
364
+ "instructions": "optional system/developer instruction",
365
+ "tools": [],
366
+ "tool_choice": "auto",
367
+ "text": {
368
+ "format": {
369
+ "type": "text"
370
+ }
371
+ },
372
+ "stream": false,
373
+ "max_output_tokens": 256
374
+ }
375
+ ```
376
+
377
+ #### Authentication (OpenAI Endpoints)
378
+
379
+ Use Bearer authentication:
380
+
381
+ ```
382
+ Authorization: Bearer API_KEY
383
+ ```
384
+
385
+ #### Tool Calling
386
+
387
+ ## Tool Calling API
388
+
389
+ Industry-leading tool calling for self-hosted AI stacks, with production-ready reliability for text and fused modality (
390
+ vision) models. One of the most robust OpenAI-compatible tool-calling implementations available on an API platform you
391
+ can own.
392
+
393
+ ### Global OpenAI Compatibility
394
+
395
+ - Auth header: `Authorization: Bearer <api_key>` is required.
396
+ - Model matching is case-insensitive against workbench `name` or `nickname`, and only models available to the API key
397
+ are usable.
398
+
399
+ #### Error Envelope
400
+
401
+ ```json
402
+ {
403
+ "error": {
404
+ "message": "....",
405
+ "type": "invalid_request_error",
406
+ "code": "...."
407
+ }
408
+ }
409
+ ```
410
+
411
+ ### POST /v1/chat/completions
412
+
413
+ #### Supported Request Fields
414
+
415
+ `model`, `messages`, `tools`, `tool_choice`, `stream`, `response_format`, `stop`, `max_tokens`, `temperature`, `top_p`,
416
+ `presence_penalty`, `frequency_penalty`, `user`. `n` is accepted but currently returns one choice (`index: 0`).
417
+
418
+ #### Request Body Example
419
+
420
+ ```json
421
+ {
422
+ "model": "Solar Open 100B",
423
+ "messages": [
424
+ {
425
+ "role": "system",
426
+ "content": "You are a helpful assistant."
427
+ },
428
+ {
429
+ "role": "user",
430
+ "content": "What is the weather in Denver?"
431
+ }
432
+ ],
433
+ "tools": [
434
+ {
435
+ "type": "function",
436
+ "function": {
437
+ "name": "get_weather",
438
+ "description": "Get current weather by city",
439
+ "parameters": {
440
+ "type": "object",
441
+ "properties": {
442
+ "city": {
443
+ "type": "string"
444
+ }
445
+ },
446
+ "required": [
447
+ "city"
448
+ ]
449
+ }
450
+ }
451
+ }
452
+ ],
453
+ "tool_choice": "auto",
454
+ "stream": false
455
+ }
456
+ ```
457
+
458
+ #### Tool Support Rules
459
+
460
+ - Only tools with `type: "function"` are used.
461
+ - Forced `tool_choice` names must exist in `tools`, or requests fail with `400 invalid_tool_choice`.
462
+ - Text and fused modality (image-text-text) models support the tool-calling pipeline. Audio models do not support tools
463
+ or streaming.
464
+ - Tool arguments are normalized to JSON strings; strict invalid arguments fail with `400 invalid_tool_call`.
465
+
466
+ #### Response Body Example
467
+
468
+ ```json
469
+ {
470
+ "choices": [
471
+ {
472
+ "index": 0,
473
+ "message": {
474
+ "role": "assistant",
475
+ "tool_calls": [
476
+ {
477
+ "id": "call_abc123",
478
+ "type": "function",
479
+ "function": {
480
+ "name": "get_weather",
481
+ "arguments": "{\"city\":\"Denver\"}"
482
+ }
483
+ }
484
+ ]
485
+ },
486
+ "finish_reason": "tool_calls"
487
+ }
488
+ ]
489
+ }
490
+ ```
491
+
492
+ #### Streaming Behavior (SSE)
493
+
494
+ 1. Emits `chat.completion.chunk` events.
495
+ 2. Streams content via `choices[0].delta.content`.
496
+ 3. Tool calls stream incrementally through `delta.tool_calls` argument chunks.
497
+ 4. Final chunk sets `finish_reason` to `tool_calls` or `stop`, then emits `[DONE]`.
498
+
499
+ ### POST /v1/responses
500
+
501
+ #### Supported Request Fields
502
+
503
+ `model`, `input`, `messages`, `input_content`, `instructions`, `text`, `tools`, `tool_choice`, `stream`, `stop`,
504
+ `max_tokens`, `max_output_tokens`, `temperature`, `top_p`, `presence_penalty`, `frequency_penalty`, `user`. `n`,
505
+ `logit_bias`, and `input_type` are accepted but not used in generation logic.
506
+
507
+ #### Request Body Example
508
+
509
+ ```json
510
+ {
511
+ "model": "Solar Open 100B",
512
+ "input": [
513
+ {
514
+ "type": "message",
515
+ "role": "user",
516
+ "content": "Find today's top headline"
517
+ }
518
+ ],
519
+ "tools": [
520
+ {
521
+ "type": "function",
522
+ "function": {
523
+ "name": "search_news",
524
+ "description": "Search current headlines",
525
+ "parameters": {
526
+ "type": "object",
527
+ "properties": {
528
+ "query": {
529
+ "type": "string"
530
+ }
531
+ },
532
+ "required": [
533
+ "query"
534
+ ]
535
+ }
536
+ }
537
+ }
538
+ ],
539
+ "tool_choice": "auto",
540
+ "stream": false
541
+ }
542
+ ```
543
+
544
+ #### Input Normalization
545
+
546
+ - `input` can be a string or a list of typed items.
547
+ - Supported item types: `message`, `input_text`, `function_call`, `function_call_output`.
548
+ - `reasoning` input items are rejected with `400 invalid_input`.
549
+
550
+ #### Response Body Example
551
+
552
+ ```json
553
+ {
554
+ "object": "response",
555
+ "output": [
556
+ {
557
+ "id": "msg_123",
558
+ "type": "message",
559
+ "status": "completed",
560
+ "role": "assistant",
561
+ "content": [
562
+ {
563
+ "type": "output_text",
564
+ "text": "Searching now...",
565
+ "annotations": []
566
+ }
567
+ ]
568
+ },
569
+ {
570
+ "id": "fc_123",
571
+ "type": "function_call",
572
+ "status": "completed",
573
+ "call_id": "call_abc123",
574
+ "name": "search_news",
575
+ "arguments": "{\"query\":\"top headline today\"}"
576
+ }
577
+ ]
578
+ }
579
+ ```
580
+
581
+ #### Streaming Behavior (Responses SSE Events)
582
+
583
+ `response.created`, `response.in_progress`, `response.output_item.added`, `response.content_part.added`,
584
+ `response.output_text.delta`, `response.output_text.done`, `response.content_part.done`,
585
+ `response.function_call_arguments.delta`, `response.function_call_arguments.done`, `response.output_item.done`,
586
+ `response.completed`, `error`
587
+
588
+ Streams end with `data: [DONE]`.
589
+
590
+ #### Multimodal Restrictions on /v1/responses
591
+
592
+ - For `audio-*` models, `stream` and `tools` are not supported.
593
+ - When provided for audio models, `text.format` must be plain text.
594
+
595
+ #### Tool Calling Parity Notes
596
+
597
+ - OpenAI-style behavior with function tools only.
598
+ - Tool arguments must normalize to valid JSON strings.
599
+ - Audio models do not support tools and/or streaming. Text and fused modality (image-text-text) models have full
600
+ tool-calling parity.
601
+
602
+ #### Whisper API
603
+
604
+ ## Whisper API
605
+
606
+ OpenAI-compatible Whisper transcription and translation endpoints built for production automation workflows.
607
+
608
+ ### Implemented Endpoints
609
+
610
+ - `POST /v1/audio/transcriptions`
611
+ - `POST /v1/audio/translations`
612
+ - `/v1/audio/speech` is not currently implemented.
613
+
614
+ ### Common Request Behavior
615
+
616
+ - Multipart upload with required `file` and `model`.
617
+ - Allowed extensions: `.mp3`, `.mp4`, `.mpeg`, `.mpga`, `.m4a`, `.wav`, `.webm`. Max size: 25 MB.
618
+ - Error mapping: unsupported format → `invalid_audio`, too large → `invalid_request_error`, invalid format value →
619
+ `invalid_response_format`.
620
+ - `model=whisper-1` maps to `UCE_WHISPER_MODEL` (default `mlx-community/whisper-large-v3-turbo`); other model names pass
621
+ through unchanged.
622
+
623
+ ### POST /v1/audio/transcriptions
624
+
625
+ #### Request Example (multipart)
626
+
627
+ ```bash
628
+ curl -X POST "$BASE_URL/v1/audio/transcriptions" \
629
+ -H "Authorization: Bearer $API_KEY" \
630
+ -F "file=@audio.wav" \
631
+ -F "model=whisper-1" \
632
+ -F "response_format=verbose_json" \
633
+ -F "timestamp_granularities[]=word"
634
+ ```
635
+
636
+ #### Response Example (json)
637
+
638
+ ```json
639
+ {
640
+ "text": "Hello from Courier Whisper."
641
+ }
642
+ ```
643
+
644
+ #### Response Example (verbose_json)
645
+
646
+ ```json
647
+ {
648
+ "text": "Hello from Courier Whisper.",
649
+ "language": "en",
650
+ "segments": [
651
+ {
652
+ "id": 0,
653
+ "start": 0.0,
654
+ "end": 1.8,
655
+ "text": "Hello from Courier Whisper."
656
+ }
657
+ ]
658
+ }
659
+ ```
660
+
661
+ > `timestamp_granularities` values `segment` and `word` are only allowed when `response_format=verbose_json`.
662
+
663
+ ### POST /v1/audio/translations
664
+
665
+ #### Request Example (multipart)
666
+
667
+ ```bash
668
+ curl -X POST "$BASE_URL/v1/audio/translations" \
669
+ -H "Authorization: Bearer $API_KEY" \
670
+ -F "file=@audio-es.mp3" \
671
+ -F "model=whisper-1" \
672
+ -F "response_format=json"
673
+ ```
674
+
675
+ #### Response Example
676
+
677
+ ```json
678
+ {
679
+ "text": "This audio was translated into English."
680
+ }
681
+ ```
682
+
683
+ ### Behavior Notes
684
+
685
+ - Uses translate operation internally.
686
+ - `word_timestamps` is disabled for translations.
687
+ - For `verbose_json`, language defaults to `en` if upstream language is missing.
688
+
689
+ #### JSON Response Formatting
690
+
691
+ ## JSON Response Formatting
692
+
693
+ ### Structured JSON Outputs with Outlines
694
+
695
+ Courier supports guaranteed structured JSON outputs using the [Outlines](https://github.com/outlines-dev/outlines)
696
+ library. This feature enables models to generate responses that strictly adhere to a provided JSON schema through
697
+ FSM-based logit masking.
698
+
699
+ ### Overview
700
+
701
+ Courier's structured JSON output feature uses Outlines to ensure models generate responses that strictly follow your
702
+ JSON schema. This is achieved through Finite State Machine (FSM) based logit masking that constrains token generation to
703
+ only produce valid JSON matching your schema.
704
+
705
+ ### Technical Architecture
706
+
707
+ - **FSM-Based Logit Masking:** Outlines builds a Finite State Machine from your JSON schema that constrains token
708
+ generation to only produce valid JSON matching the schema.
709
+ - **Generator Caching:** The first time a schema is used, there's a 0.1-1s cold start while the FSM is compiled.
710
+ Subsequent uses are instant (cached in memory per worker).
711
+ - **Thought Field Pattern:** To prevent "probability tunneling", schemas are automatically enhanced with a "thought"or "
712
+ reasoning" field if one isn't present, allowing natural language processing before data constraints.
713
+ - **Zero-Copy Integration:** The Outlines wrapper shares the same MLX model weights in memory, providing minimal
714
+ overhead when structured output is requested.
715
+
716
+ ### Usage
717
+
718
+ Both **/v1/chat/completions** and **/inference/** endpoints use the OpenAI-compatible **response_format** parameter:
719
+
720
+ #### POST /v1/chat/completions
721
+
722
+ ```json
723
+ {
724
+ "model": "Solar Open 100B",
725
+ "messages": [
726
+ {
727
+ "role": "user",
728
+ "content": "What is 123 * 456?"
729
+ }
730
+ ],
731
+ "response_format": {
732
+ "type": "json_schema",
733
+ "json_schema": {
734
+ "schema": {
735
+ "type": "object",
736
+ "properties": {
737
+ "thought": {
738
+ "type": "string"
739
+ },
740
+ "answer": {
741
+ "type": "number"
742
+ }
743
+ },
744
+ "required": [
745
+ "thought",
746
+ "answer"
747
+ ]
748
+ }
749
+ }
750
+ }
751
+ }
752
+ ```
753
+
754
+ #### POST /inference/
755
+
756
+ ```json
757
+ {
758
+ "model_id": "uuid-here",
759
+ "model_name": "your-model",
760
+ "model_type": "text-text",
761
+ "messages": [
762
+ {
763
+ "role": "user",
764
+ "content": "Classify: 'Great product!'"
765
+ }
766
+ ],
767
+ "temperature": 0.7,
768
+ "response_format": {
769
+ "type": "json_schema",
770
+ "json_schema": {
771
+ "schema": {
772
+ "type": "object",
773
+ "properties": {
774
+ "reasoning": {
775
+ "type": "string"
776
+ },
777
+ "sentiment": {
778
+ "type": "string",
779
+ "enum": [
780
+ "positive",
781
+ "negative",
782
+ "neutral"
783
+ ]
784
+ }
785
+ },
786
+ "required": [
787
+ "reasoning",
788
+ "sentiment"
789
+ ]
790
+ }
791
+ }
792
+ }
793
+ }
794
+ ```
795
+
796
+ #### Authentication
797
+
798
+ For the Courier **/inference/** endpoint, use token authentication:
799
+
800
+ ```
801
+ Authorization: API_KEY
802
+ ```
803
+
804
+ For the OpenAI **/v1/chat/completions** endpoint, use bearer authentication:
805
+
806
+ ```
807
+ Authorization: Bearer API_KEY
808
+ ```
809
+
810
+ #### Example Structured Response
811
+
812
+ ```json
813
+ {
814
+ "content": "{\n \"reasoning\": \"The user sent a positive sentiment message: 'Great product!'. I need to classify this as positive sentiment.\",\n \"sentiment\": \"positive\"\n }"
815
+ }
816
+ ```
817
+
818
+ As you can see, the LLM responded in the specified JSON structure with both reasoning and sentiment fields. You can
819
+ parse this response using `JSON.parse()` without any extra formatting or validation needed.
820
+
821
+ ### Schema Examples
822
+
823
+ #### Simple Classification
824
+
825
+ ```json
826
+ {
827
+ "type": "object",
828
+ "properties": {
829
+ "thought": {
830
+ "type": "string"
831
+ },
832
+ "classification": {
833
+ "type": "string",
834
+ "enum": [
835
+ "urgent",
836
+ "normal",
837
+ "low_priority"
838
+ ]
839
+ }
840
+ },
841
+ "required": [
842
+ "thought",
843
+ "classification"
844
+ ]
845
+ }
846
+ ```
847
+
848
+ #### Nested Objects
849
+
850
+ ```json
851
+ {
852
+ "type": "object",
853
+ "properties": {
854
+ "analysis": {
855
+ "type": "string"
856
+ },
857
+ "person": {
858
+ "type": "object",
859
+ "properties": {
860
+ "name": {
861
+ "type": "string"
862
+ },
863
+ "age": {
864
+ "type": "integer"
865
+ },
866
+ "email": {
867
+ "type": "string"
868
+ }
869
+ },
870
+ "required": [
871
+ "name",
872
+ "age"
873
+ ]
874
+ }
875
+ },
876
+ "required": [
877
+ "analysis",
878
+ "person"
879
+ ]
880
+ }
881
+ ```
882
+
883
+ #### Arrays and Lists
884
+
885
+ ```json
886
+ {
887
+ "type": "object",
888
+ "properties": {
889
+ "reasoning": {
890
+ "type": "string"
891
+ },
892
+ "items": {
893
+ "type": "array",
894
+ "items": {
895
+ "type": "object",
896
+ "properties": {
897
+ "name": {
898
+ "type": "string"
899
+ },
900
+ "quantity": {
901
+ "type": "integer"
902
+ }
903
+ },
904
+ "required": [
905
+ "name",
906
+ "quantity"
907
+ ]
908
+ }
909
+ }
910
+ },
911
+ "required": [
912
+ "reasoning",
913
+ "items"
914
+ ]
915
+ }
916
+ ```
917
+
918
+ #### Enums and Constraints
919
+
920
+ ```json
921
+ {
922
+ "type": "object",
923
+ "properties": {
924
+ "thought": {
925
+ "type": "string"
926
+ },
927
+ "rating": {
928
+ "type": "integer",
929
+ "minimum": 1,
930
+ "maximum": 5
931
+ },
932
+ "category": {
933
+ "type": "string",
934
+ "enum": [
935
+ "electronics",
936
+ "clothing",
937
+ "food",
938
+ "other"
939
+ ]
940
+ }
941
+ },
942
+ "required": [
943
+ "thought",
944
+ "rating",
945
+ "category"
946
+ ]
947
+ }
948
+ ```
949
+
950
+ ### Limitations
951
+
952
+ - **No Streaming:** Structured output currently doesn't support streaming. The response is returned as a complete JSON
953
+ object.
954
+ - **Schema Complexity:** Extremely complex schemas with deep nesting may take longer to compile or potentially fail.
955
+ - **Model Capabilities:** The underlying model must be capable of understanding and following instructions. Smaller
956
+ models may struggle with complex schemas.
957
+ - **Text & Vision Models:** Supported for `text-text` and `image-text-text` (fused modality) model types. Audio and
958
+ image generation models use standard unconstrained generation.
959
+
960
+ ### Backward Compatibility
961
+
962
+ When no `response_format` is provided, the system works exactly as it did before. There is zero impact on existing
963
+ inference flows. This feature is purely opt-in.
964
+
965
+ ```json
966
+ // This still works exactly as before
967
+ {
968
+ "model": "your-model",
969
+ "messages": [
970
+ {
971
+ "role": "user",
972
+ "content": "Hello!"
973
+ }
974
+ ]
975
+ // No response_format = standard unconstrained generation
976
+ }
977
+ ```
978
+
979
+ #### Web search
980
+
981
+ ## Web Search
982
+
983
+ Built-in web search that lets models automatically ground responses with real-time information from the web. Powered by
984
+ the Brave Search API.
985
+
986
+ When enabled, models can decide to search the web mid-inference. The server handles the search transparently and returns
987
+ a grounded response - no extra client-side logic required.
988
+
989
+ ### Setup
990
+
991
+ #### 1. Get a Brave Search API Key
992
+
993
+ Sign up at [brave.com/search/api](https://brave.com/search/api/) to get your API key. New accounts receive $5/month in
994
+ free credits (~1,000 searches).
995
+
996
+ #### 2. Configure the Key
997
+
998
+ - **Option A: Courier TUI Installer** - Run `courier` and enter your key in the "Brave Search API Key" field (in the
999
+ System Configuration section, below the ngrok fields).
1000
+ - **Option B: Manual** - Add to your `~/.courier/.env`:
1001
+
1002
+ ```bash
1003
+ BRAVE_SEARCH_API_KEY=your_key_here
1004
+ ```
1005
+
1006
+ Then restart Courier.
1007
+
1008
+ ### Usage
1009
+
1010
+ Include `web_search` in your request's `tools` array. Two formats are supported:
1011
+
1012
+ #### Shorthand Format
1013
+
1014
+ ```json
1015
+ {
1016
+ "model": "Qwen3 30B",
1017
+ "messages": [
1018
+ {
1019
+ "role": "user",
1020
+ "content": "What happened in the news today?"
1021
+ }
1022
+ ],
1023
+ "tools": [
1024
+ {
1025
+ "type": "web_search"
1026
+ }
1027
+ ]
1028
+ }
1029
+ ```
1030
+
1031
+ #### Standard Function Format
1032
+
1033
+ ```json
1034
+ {
1035
+ "model": "Qwen3 30B",
1036
+ "messages": [
1037
+ {
1038
+ "role": "user",
1039
+ "content": "What is the current price of Bitcoin?"
1040
+ }
1041
+ ],
1042
+ "tools": [
1043
+ {
1044
+ "type": "function",
1045
+ "function": {
1046
+ "name": "web_search",
1047
+ "description": "Search the web for current information",
1048
+ "parameters": {
1049
+ "type": "object",
1050
+ "properties": {
1051
+ "query": {
1052
+ "type": "string"
1053
+ }
1054
+ },
1055
+ "required": [
1056
+ "query"
1057
+ ]
1058
+ }
1059
+ }
1060
+ }
1061
+ ]
1062
+ }
1063
+ ```
1064
+
1065
+ #### cURL Example
1066
+
1067
+ ```bash
1068
+ curl -X POST http://localhost:9100/v1/chat/completions \
1069
+ -H "Content-Type: application/json" \
1070
+ -H "Authorization: Bearer YOUR_API_KEY" \
1071
+ -d '{
1072
+ "model": "Qwen3 30B",
1073
+ "messages": [{"role": "user", "content": "What are the latest developments in AI?"}],
1074
+ "tools": [{"type": "web_search"}]
1075
+ }'
1076
+ ```
1077
+
1078
+ #### Streaming
1079
+
1080
+ ```bash
1081
+ curl -X POST http://localhost:9100/v1/chat/completions \
1082
+ -H "Content-Type: application/json" \
1083
+ -H "Authorization: Bearer YOUR_API_KEY" \
1084
+ -d '{
1085
+ "model": "Qwen3 30B",
1086
+ "messages": [{"role": "user", "content": "What are the latest developments in AI?"}],
1087
+ "tools": [{"type": "web_search"}],
1088
+ "stream": true
1089
+ }'
1090
+ ```
1091
+
1092
+ Streaming works with web search enabled. Add `"stream": true` to your request. The server resolves all searches before
1093
+ streaming the final grounded response.
1094
+
1095
+ ### Mixing Web Search with Other Tools
1096
+
1097
+ Web search works alongside your own function tools. The server executes `web_search` calls automatically while returning
1098
+ your custom tool calls to the client as normal.
1099
+
1100
+ #### Example
1101
+
1102
+ ```json
1103
+ {
1104
+ "model": "Qwen3 30B",
1105
+ "messages": [
1106
+ {
1107
+ "role": "user",
1108
+ "content": "What's the weather in Denver and what's trending on Hacker News?"
1109
+ }
1110
+ ],
1111
+ "tools": [
1112
+ {
1113
+ "type": "web_search"
1114
+ },
1115
+ {
1116
+ "type": "function",
1117
+ "function": {
1118
+ "name": "get_weather",
1119
+ "description": "Get current weather by city",
1120
+ "parameters": {
1121
+ "type": "object",
1122
+ "properties": {
1123
+ "city": {
1124
+ "type": "string"
1125
+ }
1126
+ },
1127
+ "required": [
1128
+ "city"
1129
+ ]
1130
+ }
1131
+ }
1132
+ }
1133
+ ]
1134
+ }
1135
+ ```
1136
+
1137
+ In this case, the model may call both tools. `web_search` is resolved server-side and `get_weather` is returned to the
1138
+ client for execution.
1139
+
1140
+ ### How It Works
1141
+
1142
+ 1. The model decides whether a search is needed based on the user's question
1143
+ 2. If the model calls `web_search`, the server intercepts the call and queries the Brave Search API
1144
+ 3. The top 5 results (title, URL, description) are injected back as context
1145
+ 4. The model generates a final grounded response using the search results
1146
+ 5. The client receives only the final answer — the search loop is invisible
1147
+
1148
+ The server caps search iterations at 3 per request to prevent runaway loops.
1149
+
1150
+ ### Behavior Without a Key
1151
+
1152
+ If `BRAVE_SEARCH_API_KEY` is not configured and a request includes `web_search`, the tool is silently dropped. The
1153
+ request proceeds normally as if no tools were provided. A warning is logged server-side.
1154
+
1155
+ ### Pricing
1156
+
1157
+ Brave Search API charges $5 per 1,000 queries. New accounts get $5/month in free credits. There is no markup from
1158
+ Courier — you pay Brave directly for what you use.