Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

reasoning-config.test.ts

586 lines19.1 KB

1import { afterEach, describe, expect, it, vi } from 'vitest'
2​
3import { takeIngestEvents } from '../ingest/ingest-signals.ts'
4import {
5 groqModelReasoning,
6 keepValidReasoning,
7 openRouterReasoning,
8 parseByteplusReasoning,
9 parseCohereReasoning,
10 parseGroqPageEfforts,
11 parseGroqReasoning,
12 parseMistralReasoning,
13} from './reasoning-config.ts'
14import { docFixtureHtml } from './fixtures/byteplus-doc.ts'
15import { docsRun } from './model-facts.ts'
16import { reasoningViolation } from './types.ts'
17​
18describe('reasoningViolation', () => {
19 it.each([
20 { mode: 'effort', mandatory: true },
21 { mode: 'effort', mandatory: null, efforts: ['low', 'max'] },
22 { mode: 'effort', mandatory: false, efforts: ['none', 'low'] },
23 { mode: 'effort', mandatory: null, efforts: ['none', 'low'] },
24 { mode: 'adaptive', mandatory: false },
25 // One prod budget row carries efforts; that is allowed.
26 { mode: 'budget', mandatory: false, efforts: ['low'] },
27 { mode: 'toggle', mandatory: false },
28 { mode: 'toggle', mandatory: true },
29 ])('accepts %j', (value) => {
30 expect(reasoningViolation(value)).toBeNull()
31 })
32​
33 it.each([
34 ['a non-object', 'effort', 'not an object'],
35 ['an unknown mode', { mode: 'switch', mandatory: false }, 'unknown mode'],
36 ['a missing mandatory', { mode: 'effort' }, 'mandatory is not'],
37 ['a string mandatory', { mode: 'effort', mandatory: 'no' }, 'mandatory'],
38 [
39 'a toggle with an unstated mandatory',
40 { mode: 'toggle', mandatory: null },
41 'a toggle needs a stated mandatory',
42 ],
43 [
44 'efforts on a toggle',
45 { mode: 'toggle', mandatory: false, efforts: ['low'] },
46 'a toggle takes no efforts',
47 ],
48 [
49 'empty efforts',
50 { mode: 'effort', mandatory: true, efforts: [] },
51 'efforts',
52 ],
53 [
54 'a non-string effort',
55 { mode: 'effort', mandatory: true, efforts: ['low', 1] },
56 'efforts',
57 ],
58 [
59 'a mandatory true with a none effort',
60 { mode: 'effort', mandatory: true, efforts: ['none', 'low'] },
61 'mandatory is true but efforts include none',
62 ],
63 [
64 'null efforts',
65 { mode: 'effort', mandatory: true, efforts: null },
66 'efforts',
67 ],
68 ])('refuses %s', (_case, value, reason) => {
69 expect(reasoningViolation(value)).toContain(reason)
70 })
71})
72​
73describe('keepValidReasoning', () => {
74 const source = { derivation: 'listing' as const, sourceUrl: 'https://x.test' }
75​
76 it('passes a valid or absent object through untouched', () => {
77 const valid = {
78 rawId: 'm',
79 reasoning: { mode: 'toggle', mandatory: false },
80 }
81 expect(keepValidReasoning('p', valid as never)).toBe(valid)
82 const none = { rawId: 'm', reasoning: null }
83 expect(keepValidReasoning('p', none)).toBe(none)
84 expect(takeIngestEvents()).toEqual([])
85 })
86​
87 it('keeps the prior value and source, and reports the refusal', () => {
88 const prior = {
89 reasoning: { mode: 'effort', mandatory: true },
90 factSources: { reasoning: { ...source, path: 'old' } },
91 }
92 const kept = keepValidReasoning(
93 'prov',
94 {
95 rawId: 'm-1',
96 reasoning: { mode: 'toggle', mandatory: null },
97 factSources: { reasoning: source, contextWindow: source },
98 },
99 prior,
100 )
101 expect(kept.reasoning).toEqual(prior.reasoning)
102 expect(kept.factSources).toEqual({
103 contextWindow: source,
104 reasoning: prior.factSources.reasoning,
105 })
106 expect(takeIngestEvents()).toEqual([
107 {
108 event: 'ingest_failed',
109 properties: {
110 job: 'models-poll',
111 providerId: 'prov',
112 error: 'm-1: reasoning not stored: a toggle needs a stated mandatory',
113 },
114 },
115 ])
116 })
117​
118 it('keeps the prior value over a mandatory true with a none effort', () => {
119 const prior = {
120 reasoning: { mode: 'effort', mandatory: false, efforts: ['none', 'low'] },
121 factSources: null,
122 }
123 const kept = keepValidReasoning(
124 'prov',
125 {
126 rawId: 'm-3',
127 reasoning: {
128 mode: 'effort',
129 mandatory: true,
130 efforts: ['none', 'low'],
131 },
132 },
133 prior,
134 )
135 expect(kept.reasoning).toEqual(prior.reasoning)
136 expect(takeIngestEvents()).toEqual([
137 {
138 event: 'ingest_failed',
139 properties: {
140 job: 'models-poll',
141 providerId: 'prov',
142 error:
143 'm-3: reasoning not stored: mandatory is true but efforts include none',
144 },
145 },
146 ])
147 })
148​
149 it('stores nothing on a new row', () => {
150 const kept = keepValidReasoning('prov', {
151 rawId: 'm-2',
152 reasoning: { mode: 'toggle', mandatory: null },
153 factSources: { reasoning: source },
154 })
155 expect(kept.reasoning).toBeNull()
156 expect(kept.factSources).toBeUndefined()
157 takeIngestEvents()
158 })
159})
160​
161const MISTRAL = `
162# Reasoning
163​
164- \`mistral-small-latest\`: Supports adjustable reasoning via the \`reasoning_effort\` parameter. No extra configuration required.
165- \`mistral-medium-3-5\`: Supports adjustable reasoning via the \`reasoning_effort\` parameter.
166- \`zai-glm-5-3\`: Supports adjustable reasoning via the \`reasoning_effort\` parameter. Supported values are \`low\`, \`high\`, and \`max\` (see note below).
167​
168- \`reasoning_effort = "high"\`: full thinking chunk.
169- \`reasoning_effort = "none"\`: thinking chunk omitted.
170​
171\`zai-glm-5-3\` supports \`low\`, \`high\`, and \`max\` — not \`none\`.
172​
173## Handling thinking chunks
175​
176const NATIVE = `
177# Native reasoning (deprecated)
178​
179Native reasoning models always generate thinking traces without any extra parameters:
180​
181- \`magistral-small-latest\`: open smaller version.
182- \`magistral-medium-latest\`: more powerful reasoning model.
184​
185const GROQ = `
186# Reasoning
187​
188## [Supported Models](#supported-models)
189​
190| Model ID | Model |
191| --- | --- |
192| openai/gpt-oss-120b | [OpenAI GPT-OSS 120B](https://example.com/120) |
193| openai/gpt-oss-safeguard-20b | [OpenAI GPT-OSS-Safeguard 20B](https://example.com/safe) |
194| qwen/qwen3.8-27b | [Qwen 3.8 27B](https://example.com/qwen) |
195| minimaxai/minimax-m2.7 | [MiniMax M2.7](https://example.com/mini) |
196​
197### Options for Reasoning Effort (Qwen 3.8 27B)
198​
199[Qwen 3.8 27B](https://example.com/qwen) supports the following options:
200​
201| reasoning_effort Options | Description |
202| --- | --- |
203| none | Disable reasoning. |
204| default | Default behavior. |
205| low | Low effort. |
206| medium | Medium effort. |
207| high | High effort. |
208​
209### Options for Reasoning Effort (GPT-OSS)
210​
211This is only supported by [GPT-OSS 120B](https://example.com/120).
212​
213| reasoning_effort Options | Description |
214| --- | --- |
215| low | Low effort. |
216| medium | Medium effort. |
217| high | High effort. |
219​
220const COHERE = `
221# Reasoning
222​
223\`\`\`python
224response = co.chat(
225 model="command-a-reasoning-08-2025",
226 messages=[{"role": "user", "content": "Hi"}],
228\`\`\`
229​
230thinking={
231 "type": "disabled" # turns off thinking. It is set to "enabled" by default.
233​
234thinking = {"token_budget": 500}
236​
237const COHERE_SILENT = `
238# Reasoning
239​
240model="command-r-08-2024"
241​
242Reasoning is on by default. No budget or effort parameter is documented.
244​
245// Shaped like https://docs.byteplus.com/en/docs/ModelArk/1449737 on
246// 2026-10-23: the effort table's cells are their own zones, and other
247// tables on the page list ids that are not in it.
248const BYTEPLUS = docFixtureHtml([
249 'Thinking can be switched off with thinking.type for seed-1-6-250615.',
250 '## Adjust chain-of-thought length',
251 'Use the ',
252 'reasoning_effort',
253 ' parameter. Valid values are:',
254 'none',
255 ': Turn off reasoning.',
256 'minimal',
257 'low',
258 'high',
259 'The table below shows the models that support adjusting the chain-of-thought length.',
260 [
261 ['Supported models', 'Default value', 'Compatibility mapping'],
262 ['* dola-seed-2-1-turbo-260628', '', ''],
263 [
264 'seed-2-0-lite-260228 * glm-5-2-260617',
265 'medium',
266 'If none is passed, reasoning is turned off.',
267 ],
268 ],
269 'Supported models:',
270 'deepseek-v4-flash-260425',
271 [
272 ['API', 'Chat API'],
273 ['Supported models', 'deepseek-v4-pro-260425 deepseek-v4-flash-260425'],
274 ],
275 '## Pass thinking content back',
276 'seed-1-8-251228',
277])
278​
279describe('openRouterReasoning', () => {
280 it('copies published efforts and does not invent a mode', () => {
281 expect(
282 openRouterReasoning({
283 reasoning: {
284 supported_efforts: ['high', 'low', null],
285 mandatory: false,
286 },
287 }),
288 ).toEqual({
289 mode: 'effort',
290 mandatory: false,
291 efforts: ['high', 'low'],
292 })
293 expect(
294 openRouterReasoning({
295 reasoning: { supported_efforts: null, mandatory: true },
296 }),
297 ).toEqual({ mode: 'effort', mandatory: true })
298 expect(
299 openRouterReasoning({
300 reasoning: { supports_max_tokens: true, mandatory: true },
301 }),
302 ).toEqual({ mode: 'budget', mandatory: true })
303 expect(
304 openRouterReasoning({
305 reasoning: { mandatory: true },
306 }),
307 ).toBeNull()
308 expect(
309 openRouterReasoning({
310 reasoning: null,
311 }),
312 ).toBeNull()
313 })
314})
315​
316describe('parseMistralReasoning', () => {
317 it('reads effort names the guide states, and leaves native models unset', () => {
318 const parsed = parseMistralReasoning(MISTRAL)
319 expect(parsed.get('mistral-small-latest')).toEqual({
320 mode: 'effort',
321 mandatory: false,
322 })
323 expect(parsed.get('mistral-medium-3-5')).toEqual({
324 mode: 'effort',
325 mandatory: false,
326 })
327 expect(parsed.get('mistral-medium-3-5')?.mode).toBe('effort')
328 expect(parsed.get('zai-glm-5-3')).toEqual({
329 mode: 'effort',
330 mandatory: null,
331 efforts: ['low', 'high', 'max'],
332 })
333 expect(parseMistralReasoning(NATIVE).get('magistral-medium-latest')).toBe(
334 undefined,
335 )
336 })
337​
338 it('reads the rendered guide, including a model whose own levels replace the shared list', () => {
339 const html = `<!DOCTYPE html><html><main>
340<ul>
341<li>Mistral Small 4 (<code>mistral-small-2603</code>): Supports adjustable reasoning via the <code>reasoning_effort</code> parameter.</li>
342<li>Mistral Medium 3.5 (<code>mistral-medium-3-5</code>): Supports adjustable reasoning via the <code>reasoning_effort</code> parameter.</li>
343<li>Mistral Large 4 (<code>mistral-large-4-0</code>): Supports <code>low</code>, <code>medium</code>, and <code>high</code> reasoning effort.</li>
344<li><code>zai-glm-5-3</code>: Supports adjustable reasoning via the <code>reasoning_effort</code> parameter. Supported values are <code>low</code>, <code>high</code>, and <code>max</code> (see note below).</li>
345<li>GLM 5.3 Flash (<code>zai-glm-5-3-flash</code>): Supports <code>low</code>, <code>high</code>, and <code>max</code> reasoning effort. <code>none</code> is not supported.</li>
346</ul>
347<p>The <code>reasoning_effort</code> parameter controls how much compute the model spends reasoning. For <code>mistral-small-2603</code> and <code>mistral-medium-3-5</code>:</p>
348<ul>
349<li><code>reasoning_effort = &quot;high&quot;</code>: The response includes a full thinking chunk before the final answer.</li>
350<li><code>reasoning_effort = &quot;none&quot;</code>: The model thinks minimally and the thinking chunk is omitted from the response.</li>
351</ul>
352<p><code>zai-glm-5-3</code> supports <code>low</code>, <code>high</code>, and <code>max</code> — not <code>none</code>. <code>message.content</code> is always returned as a list of chunks (thinking + text).</p>
353<p>described in Handling thinking chunks.</p>
354<h3>Mistral Large 4 reasoning effort</h3>
355<p>Mistral Large 4 (<code>mistral-large-4</code> / <code>mistral-large-4-0</code>) supports three reasoning levels:</p>
356<p>Mistral Large 4 always reasons, so no level matches <code>none</code>.</p>
357<h2>Usage</h2>
358</main></html>`
359 const parsed = parseMistralReasoning(html)
360 expect(parsed.get('mistral-small-2603')).toEqual({
361 mode: 'effort',
362 mandatory: false,
363 efforts: ['high', 'none'],
364 })
365 expect(parsed.get('mistral-medium-3-5')).toEqual({
366 mode: 'effort',
367 mandatory: false,
368 efforts: ['high', 'none'],
369 })
370 expect(parsed.get('zai-glm-5-3')).toEqual({
371 mode: 'effort',
372 mandatory: true,
373 efforts: ['low', 'high', 'max'],
374 })
375 expect(parsed.get('zai-glm-5-3-flash')).toEqual({
376 mode: 'effort',
377 mandatory: null,
378 efforts: ['low', 'high', 'max'],
379 })
380 expect(parsed.get('mistral-large-4-0')).toEqual({
381 mode: 'effort',
382 mandatory: true,
383 efforts: ['low', 'medium', 'high'],
384 })
385 expect(parsed.get('mistral-large-4')).toEqual(
386 parsed.get('mistral-large-4-0'),
387 )
388 })
389})
390​
391describe('parseGroqReasoning', () => {
392 it('reads each effort table and leaves models with no options unset', () => {
393 const parsed = parseGroqReasoning(GROQ)
394 expect(parsed.get('qwen/qwen3.8-27b')).toEqual({
395 mode: 'effort',
396 mandatory: false,
397 efforts: ['none', 'default', 'low', 'medium', 'high'],
398 })
399 expect(parsed.get('openai/gpt-oss-120b')).toEqual({
400 mode: 'effort',
401 mandatory: true,
402 efforts: ['low', 'medium', 'high'],
403 })
404 expect(parsed.get('openai/gpt-oss-safeguard-20b')).toBeUndefined()
405 expect(parsed.get('minimaxai/minimax-m2.7')).toBeUndefined()
406 })
407})
408​
409describe('parseCohereReasoning', () => {
410 it('uses token_budget as budget mode and stays null without that knob', () => {
411 expect(
412 parseCohereReasoning(COHERE).get('command-a-reasoning-08-2025'),
413 ).toEqual({ mode: 'budget', mandatory: false })
414 expect(
415 parseCohereReasoning(COHERE_SILENT).get('command-r-08-2024'),
416 ).toBeUndefined()
417 })
418​
419 it('covers every reasoning model only while the guide calls them hybrid', () => {
420 // The sentence as https://docs.cohere.com/docs/reasoning.md had it on
421 // 2026-10-07.
422 const hybrid = `Cohere's reasoning models are *hybrid*, meaning reasoning can be enabled (in which case they generate internal reasoning processes before delivering their final responses) or disabled (in which case they function the way any other LLM would).`
423 expect(parseCohereReasoning(COHERE).has('*')).toBe(false)
424 expect(parseCohereReasoning(`${hybrid}\n${COHERE}`).get('*')).toEqual({
425 mode: 'budget',
426 mandatory: false,
427 })
428 expect(
429 parseCohereReasoning(
430 `${hybrid.replace('or disabled', 'but not disabled')}\n${COHERE}`,
431 ).has('*'),
432 ).toBe(false)
433 // Without the budget knob the sentence configures nothing.
434 expect(parseCohereReasoning(`${hybrid}\n${COHERE_SILENT}`).size).toBe(0)
435 })
436})
437​
438describe('parseByteplusReasoning', () => {
439 it('copies the published effort list onto supported-model ids only', () => {
440 const parsed = parseByteplusReasoning(BYTEPLUS)
441 expect(parsed.get('seed-2-0-lite-260228')).toEqual({
442 mode: 'effort',
443 mandatory: false,
444 efforts: ['none', 'minimal', 'low', 'high'],
445 })
446 expect(parsed.get('dola-seed-2-1-turbo-260628')?.mode).toBe('effort')
447 expect(parsed.get('glm-5-2-260617')?.efforts).toEqual([
448 'none',
449 'minimal',
450 'low',
451 'high',
452 ])
453 expect(parsed.get('seed-1-6-250615')).toBeUndefined()
454 expect(parsed.get('deepseek-v4-flash-260425')).toBeUndefined()
455 expect(parsed.get('seed-1-8-251228')).toBeUndefined()
456 expect(parsed.size).toBe(3)
457 })
458​
459 it('configures nothing when the effort table is gone', () => {
460 expect(
461 parseByteplusReasoning(
462 docFixtureHtml(['## Adjust chain-of-thought length', 'none', 'high']),
463 ).size,
464 ).toBe(0)
465 })
466​
467 it('takes effort names only from the list before the table', () => {
468 const parsed = parseByteplusReasoning(
469 docFixtureHtml([
470 '## Adjust chain-of-thought length',
471 'high',
472 [['Supported models'], ['seed-2-0-lite-260228']],
473 'max',
474 ]),
475 )
476 expect(parsed.get('seed-2-0-lite-260228')?.efforts).toEqual(['high'])
477 })
478})
479​
480describe('parseGroqPageEfforts', () => {
481 it('reads the model page sentence and fails closed when it changes', () => {
482 // openai/gpt-oss-safeguard-20b.md on 2026-10-23.
483 const line =
484 '* Harmony format for structured reasoning with low/medium/high reasoning effort'
485 expect(parseGroqPageEfforts(line)).toEqual(['low', 'medium', 'high'])
486 expect(parseGroqPageEfforts('Supports configurable reasoning.')).toBeNull()
487 expect(() => parseGroqPageEfforts(line.replace('medium', 'turbo'))).toThrow(
488 /unreadable reasoning effort/,
489 )
490 })
491})
492​
493describe('groqModelReasoning', () => {
494 afterEach(() => vi.unstubAllGlobals())
495​
496 const GUIDE = `
497## [Supported Models](#supported-models)
498​
499| Model ID | Model |
500| --- | --- |
501| openai/gpt-oss-20b | [GPT-OSS 20B](https://console.groq.com/docs/model/openai/gpt-oss-20b) |
502| openai/gpt-oss-safeguard-20b | [GPT-OSS-Safeguard 20B](https://console.groq.com/docs/model/openai/gpt-oss-safeguard-20b) |
503| minimaxai/minimax-m2.7 | [MiniMax M2.7](https://console.groq.com/docs/model/minimaxai/minimax-m2.7) |
504​
505### [Options for Reasoning Effort (GPT-OSS)](#gpt-oss)
506​
507Only supported by [GPT-OSS 20B](https://console.groq.com/docs/model/openai/gpt-oss-20b).
508​
509| Option | Description |
510| --- | --- |
511| low | Low effort |
512| high | High effort |
514​
515 function serve(pages: Record<string, string | null>) {
516 vi.stubGlobal(
517 'fetch',
518 vi.fn((url: string) => {
519 const body = url.endsWith('/reasoning.md') ? GUIDE : pages[url]
520 return Promise.resolve(
521 body == null
522 ? new Response('down', { status: 503 })
523 : new Response(body),
524 )
525 }),
526 )
527 }
528 const page = (id: string) => `https://console.groq.com/docs/model/${id}.md`
529 const ids = [
530 'openai/gpt-oss-20b',
531 'openai/gpt-oss-safeguard-20b',
532 'minimaxai/minimax-m2.7',
533 ]
534​
535 it('reads efforts from the model page of a guide model with no options', async () => {
536 serve({
537 [page('openai/gpt-oss-safeguard-20b')]:
538 'Harmony format with low/medium/high reasoning effort',
539 [page('minimaxai/minimax-m2.7')]: 'A reasoning model.',
540 })
541 const run = docsRun()
542 const reasoning = await groqModelReasoning(undefined, run, ids)
543 expect(reasoning('openai/gpt-oss-safeguard-20b', true)).toEqual({
544 reasoning: {
545 mode: 'effort',
546 mandatory: null,
547 efforts: ['low', 'medium', 'high'],
548 },
549 factSources: {
550 reasoning: expect.objectContaining({
551 sourceUrl: page('openai/gpt-oss-safeguard-20b'),
552 path: 'reasoning',
553 }) as unknown,
554 },
555 })
556 expect(reasoning('minimaxai/minimax-m2.7', true).reasoning).toBeUndefined()
557 expect(reasoning('openai/gpt-oss-20b', true).reasoning?.efforts).toEqual([
558 'low',
559 'high',
560 ])
561 expect(run.failed).toBe(0)
562 })
563​
564 it('keeps stored reasoning when a model page fails to load', async () => {
565 serve({ [page('minimaxai/minimax-m2.7')]: 'A reasoning model.' })
566 const run = docsRun()
567 const reasoning = await groqModelReasoning(undefined, run, ids)
568 expect(reasoning('openai/gpt-oss-safeguard-20b', true)).toEqual({
569 absent: { reasoning: 'unavailable' },
570 })
571 expect(run.failed).toBe(1)
572 })
573​
574 it('keeps stored reasoning when the guide fails to load', async () => {
575 vi.stubGlobal(
576 'fetch',
577 vi.fn(() => Promise.resolve(new Response('down', { status: 503 }))),
578 )
579 const run = docsRun()
580 const reasoning = await groqModelReasoning(undefined, run, ids)
581 expect(reasoning('openai/gpt-oss-20b', true)).toEqual({
582 absent: { reasoning: 'unavailable' },
583 })
584 expect(run.failed).toBe(1)
585 })
586})