feat(ai): configurable reasoning effort for OpenAI-compatible providers
Branch: feat/custom-provider-reasoning-effort.
Closes: #3596
Problem
Reasoning models can spend much of their output budget thinking before producing the JSON Sure requested, making batch categorization slow or prone to truncated responses. Sure needs a way to configure reasoning effort when the selected OpenAI-compatible model and endpoint support it.
Change
- Add
Setting.openai_reasoning_effortandOPENAI_REASONING_EFFORT, with ENV > Setting > unset precedence. Which values work depends on the model and endpoint (a live check againstgpt-5.4-nanoacceptedxhighbut rejectedminimalandmax), so there is no allowlist or per-model mapping. Any single lowercase word is passed through unchanged after trimming and downcasing. Malformed configured values are logged and omitted. Unset leaves the optional parameter out of requests, with no global default. - Apply the setting to OpenAI generation requests: native Responses and generic Chat Completions, categorization, merchant detection/enhancement, bill setup, bank-statement extraction, and PDF text/vision processing. Chat requests use
reasoning_effort; Responses requests usereasoning: { effort }. Direct chat/tool health probes omit it; synthetic PDF health probes use the normal PDF processor and inherit the configured effort. - Existing HTTP-400 compatibility retries in bill setup, categorization, merchant detection, and merchant enhancement remove both
response_formatandreasoning_effort. Auto-mode retries after unparseable or unusable structured output retain the configured effort; a failed fallback propagates without a third request. The strict→none retry and the auto-mode heuristic, previously copied in the three extractors, are now defined once in theJsonModeconcern (request_with_json_mode_fallback); each extractor supplies only its failed-item predicate and expected count. - Add a Reasoning effort text field in Settings → Self-hosting → OpenAI, next to the Model field and built the same way. It suggests
none,minimal,low,medium,high,xhighandmaxthrough a datalist, and blank means Provider default. It is disabled when ENV supplies the value and shows the same normalized effective value sent in requests. The controller validates the submitted value before saving hosting settings, changing securities-provider toggles, or scheduling sync, and a rejected value stays in the field. - Document the setting, model/endpoint support, ENV precedence, generation requests, direct chat/tool probes, synthetic PDF probes, and compatibility retries in
docs/hosting/ai.md,.env.example, and.env.local.example. Anthropic behavior is unchanged.
Validation
Head 7471506d0 (free-text field + shared JSON-mode fallback, merged with current main; the merge resolved only the hostings controller permit list / write order and the appended controller tests — 272 focused settings/OpenAI tests pass, no failures):
- Focused OpenAI provider, hosting-controller, and i18n tests: 316 runs, 1,503 assertions, no failures or errors, 4 conditional skips. New cases cover a value outside the suggestions (
xhigh) being saved and sent, malformed values (very high,high!, over-long) being rejected, a rejected value staying in the field, and the datalist wiring. - RuboCop and ERB lint on the changed files: no offenses.
- Follow-up for the automated review: tests asserting that
reasoning_effortreachesclient.chatfor bank-statement extraction (every chunk) and PDF text/vision processing, and is absent when unset; the OpenAI provider suite is 215 runs, 910 assertions, no failures. The docs and the fallback comments now state precisely when the 400 retry applies. - The shared-fallback refactor is behaviour-preserving: the parametrised retry suites (
generic_reasoning_retry_test.rb,combined_json_reasoning_retry_test.rb) pass unchanged; OpenAI provider + family suites: 546 runs, 2,452 assertions, no failures. - CI runs the full suite on this head.
Earlier head 4e6b0e040 (fixed-list select)
- Provider and categorization tests cover precedence, normalization, invalid values, both API payload forms, and omission when unset. Bill setup and the three generic extractors cover HTTP-400 fallbacks, retained effort on heuristic retries, bounded retries, and error propagation.
- 18 combined JSON/reasoning regression cases cover retained effort on Auto-mode fallbacks, compatibility fallbacks that omit formatting and effort, and bounded retries.
- Full Rails suite: 12,924 runs, 59,829 assertions, no failures or errors, 36 skips. Self-hosting settings browser test: 1 run, 6 assertions.
- RuboCop (3,055 files), ERB lint (776 files), and Biome (136 files) passed. Brakeman reported no errors or security warnings, with 9 existing ignored warnings.
Live API check
gpt-5.4-nano on api.openai.com, called through the production AutoCategorizer with synthetic transactions:
| Path | Effort | Result |
|---|---|---|
| Responses API | unset | 200, correct category; the response reported effort none (the model default) |
| Responses API | low | 200, correct category; the response reported effort low |
| Chat Completions (custom-provider path) | unset | 200, correct category |
| Chat Completions (custom-provider path) | low | 200, correct category on the first request, with no fallback retry |
A direct probe of the same model accepted none, low, medium, high and xhigh, and rejected minimal and max with HTTP 400 unsupported_value on reasoning.effort. That is why the fixed list was replaced. The live run used the earlier head; this update changes validation and the settings UI, not the request path.
Screenshots
Settings > Self-Hosting > OpenAI, free-text effort field. Empty (provider default, parameter not sent):
With xhigh entered:
Summary by CodeRabbit
- New Features
- Added a Reasoning effort option to OpenAI settings, with provider defaults and support for none, minimal, low, medium, and high.
- Configured reasoning effort is applied to supported OpenAI requests; environment settings take precedence.
- Bug Fixes
- Some requests rejected by the API now retry without JSON formatting or reasoning effort.
- Invalid reasoning-effort values are rejected without changing other OpenAI settings.
- Documentation
- Documented the setting, supported values, and compatibility considerations.