Skip to content

Harden OpenAI-compatible JSON responses and ensure json_object prompt hint#1368

Merged
nicoloboschi merged 1 commit intovectorize-io:mainfrom
voarsh2:pr/openai-compatible-json-response-hardening
May 4, 2026
Merged

Harden OpenAI-compatible JSON responses and ensure json_object prompt hint#1368
nicoloboschi merged 1 commit intovectorize-io:mainfrom
voarsh2:pr/openai-compatible-json-response-hardening

Conversation

@voarsh2
Copy link
Copy Markdown
Contributor

@voarsh2 voarsh2 commented Apr 30, 2026

Summary

Fixes two OpenAI-compatible provider edge cases in structured JSON calls:

  • ensure response_format={"type":"json_object"} requests include a lowercase json hint in a user message
  • surface malformed success responses with clear ProviderResponseErrors instead of crashing on response.choices[0]

Some OpenAI-compatible gateways enforce JSON mode’s “messages must contain json” requirement against the user/input message, and may return an error payload with choices: null. Previously this could become a generic TypeError: 'NoneType' object is not subscriptable, causing opaque retain extraction failures and wasted retries.

Changes

  • Adds a small JSON-mode user hint when needed: Return valid json only.
  • Detects provider error payloads in success-shaped responses
  • Detects missing choices, missing messages, and empty message content
  • Avoids retrying deterministic provider error payloads
  • Keeps retry behavior for transient/malformed response shapes
  • Adds focused unit tests for JSON hint injection and malformed provider responses

Testing

  • uv run pytest -q tests/test_openai_compatible_response_hardening.py

Ensure json_object calls include a user-message json hint, and convert
malformed success responses into clear ProviderResponseError failures
instead of crashing on missing choices/content.

This avoids opaque retain extraction TypeErrors and prevents deterministic
provider error payloads from being retried as generic chunk failures.
Copy link
Copy Markdown
Collaborator

@nicoloboschi nicoloboschi left a comment

Choose a reason for hiding this comment

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

LGTM

@nicoloboschi nicoloboschi merged commit bc14e5c into vectorize-io:main May 4, 2026
57 of 58 checks passed
liling pushed a commit to liling/hindsight that referenced this pull request May 5, 2026
Ensure json_object calls include a user-message json hint, and convert
malformed success responses into clear ProviderResponseError failures
instead of crashing on missing choices/content.

This avoids opaque retain extraction TypeErrors and prevents deterministic
provider error payloads from being retried as generic chunk failures.

Co-authored-by: Reese <reesevader@hotmail.co.uk>
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.

3 participants