Skip to content

Conversation

@jxnl
Copy link
Collaborator

@jxnl jxnl commented Jan 16, 2026

fix(genai): stop auto-sending unsupported safety categories

Describe your changes

This PR resolves a critical production blocker where Google GenAI's SafetySettings caused 400 INVALID_ARGUMENT errors, particularly with multimodal (image) content.

The root cause was that instructor was automatically sending all google.genai.types.HarmCategory enum members as safety settings, including categories unsupported for certain modalities (e.g., HARM_CATEGORY_CIVIC_INTEGRITY or image-specific categories when no image was present).

This fix refactors update_genai_kwargs to:

  • Emit only a stable baseline of safety categories by default (HARM_CATEGORY_HATE_SPEECH, HARASSMENT, DANGEROUS_CONTENT at BLOCK_ONLY_HIGH).
  • Dynamically include HARM_CATEGORY_IMAGE_* categories only when the request contains image content.
  • Properly parse and merge user-provided safety settings (from dict or list) with the default baseline.
  • Exclude UNSPECIFIED and JAILBREAK categories.

This ensures that only valid and supported safety categories are sent to the Google GenAI API, preventing the 400 INVALID_ARGUMENT errors. Thorough regression tests have been added and updated to cover these scenarios.

Issue ticket number and link

Linear: 567-261
GitHub: #1773 (#1773)

Checklist before requesting a review

  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests.
  • If it is a core feature, I have added documentation.

Linear Issue: 567-261

Open in Cursor Open in Web

Only emit a stable baseline of safety settings instead of iterating every HarmCategory enum member. This prevents 400 INVALID_ARGUMENT errors when the SDK adds categories that certain requests (notably multimodal/image) don't accept.

Co-authored-by: jason <[email protected]>
@cursor
Copy link

cursor bot commented Jan 16, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jan 16, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
instructor 2002f58 Commit Preview URL

Branch Preview URL
Jan 16 2026, 05:31 PM

@github-actions github-actions bot added bug Something isn't working priority:critical Critical issue affecting production size:M This PR changes 30-99 lines, ignoring generated files. status:pending-merge Related PR is pending merge labels Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority:critical Critical issue affecting production size:M This PR changes 30-99 lines, ignoring generated files. status:pending-merge Related PR is pending merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants