Skip to content

Keep OpenAI error descriptions when requests fail - #343

Merged
HamptonMakes merged 4 commits into
block:mainfrom
ThePumpingLemma:dgrochowski/summary-error-body
Nov 23, 2026
Merged

HamptonMakes merged 4 commits into
block:mainfrom
ThePumpingLemma:dgrochowski/summary-error-body

Conversation

@ThePumpingLemma

@ThePumpingLemma ThePumpingLemma commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

When OpenAI rejects a request, Coplan logs only Faraday's message: "the server responded with status 400". The reason is in the response body, and we drop it.

In production, SummarizePlanJob discarded 33 summaries with HTTP 400 in the last 14 days. The last 10 came from two plans. We can't tell why from the logs. A context-length limit is likely, but that's a guess.

Now AiProviders::OpenAi adds the provider's error type, code and message to the error it raises, for both chat and transcription. It accepts gateway-specific reasons without an allowlist. The combined diagnostic is limited to 300 characters and normalized to one line. Only string diagnostic fields are appended; request objects and other fields are omitted. The job log then says, for example:

Chat request failed: the server responded with status 400 (invalid_request_error/context_length_exceeded: This model supports at most 128000 tokens.)

This PR only fixes the diagnosis. Summary failure-state handling (finding identity-ai-5) remains a separate follow-up to #342.

Found in the Coplan STPA review (finding identity-ai-5): https://coplan.sqprod.co/dgrochowski/reliability-analyses/coplan-stamp-stpa-analysis-service-review

Evidence

spec/services/ai_providers/open_ai_spec.rb covers chat and transcription with decoded and JSON error bodies, gateway-specific reasons, length limits, whitespace and encoding, malformed fields, string-only gateway errors, and malformed JSON raised by Faraday's middleware. Unusable details add nothing; the original provider failure remains the exception cause.

How to try it

bin/rails runner 'Coplan.configuration.ai_model = "no-such-model"; Coplan::Ai.call(system_prompt: "Hi", user_content: "Hi")'

With a real key this raises Coplan::Ai::Error that ends with OpenAI's reason, such as (invalid_request_error/model_not_found: The model ... does not exist ...).

Testing

  • No browser UI involved.
  • Added a provider spec.
  • Focused provider, AI wrapper, summary, and dictation specs pass. CI covers the full suite.
  • Light and dark mode don't apply.
  • A network-free check through the real OpenAI client covers chat and transcription failures.

Generated with Codex

- Faraday's message is only the HTTP status; OpenAI's body says why (type, code, message).
- Append those, truncated to 300 characters, for chat and transcription. Never the request.
Signed-off-by: Hampton Lintorn-Catlin <hampton@squareup.com>
@HamptonMakes HamptonMakes changed the title Keep OpenAI's error reason when an AI request fails Keep safe OpenAI error reasons when requests fail Nov 23, 2026
@HamptonMakes

Copy link
Copy Markdown
Collaborator

I'd prefer to not hardcode acceptable error messages... we should be able to trust that the provider is going to provide safe text descriptoins of error for saving.

@HamptonMakes HamptonMakes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in comment

Signed-off-by: Hampton Lintorn-Catlin <hampton@squareup.com>
Signed-off-by: Hampton Lintorn-Catlin <hampton@squareup.com>
@HamptonMakes HamptonMakes changed the title Keep safe OpenAI error reasons when requests fail Keep OpenAI error descriptions when requests fail Nov 23, 2026
@HamptonMakes
HamptonMakes merged commit ed51a84 into block:main Nov 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants