Skip to content

fix(gateway): map google image safety finish reasons - #1340

Merged
steebchen merged 1 commit into
mainfrom
steebchen/image-safety-filter
Dec 18, 2025
Merged

steebchen merged 1 commit into
mainfrom
steebchen/image-safety-filter

Conversation

@steebchen

@steebchen steebchen commented Dec 18, 2025 •

Copy link
Copy Markdown
Member

Summary

This PR fixes the handling of Google's image-related finish reasons by properly classifying them as content filters in the unified finish reason system. Previously, finish reasons like IMAGE_SAFETY would be logged as "Unknown finish reason encountered" errors.

Changes

  • Added IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, LANGUAGE, and NO_IMAGE to the content filter mapping for google-ai-studio and google-vertex providers
  • Added corresponding unit tests for the new finish reason mappings

Testing

All unit tests pass, including the new tests for the added finish reasons.

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved accuracy of content filter detection for Google AI responses by expanding finish reason classification to recognize additional response types including language restrictions, image safety, prohibited content, and missing images
  • Tests

    • Extended test coverage to ensure Google AI finish reasons are properly classified for content filtering scenarios

✏️ Tip: You can customize this high-level summary in your review settings.

Add IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, LANGUAGE, and NO_IMAGE to the
content filter mapping for google-ai-studio and google-vertex providers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings December 18, 2025 18:21
@coderabbitai

coderabbitai Bot commented Dec 18, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Extends Google AI Studio finish-reason mappings by adding LANGUAGE, IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, and NO_IMAGE cases that map to CONTENT_FILTER. Updates both production logic in logs.ts and corresponding test cases in logs.spec.ts.

Changes

Cohort / File(s) Change Summary
Google AI Studio finish-reason mappings
apps/gateway/src/lib/logs.ts, apps/gateway/src/lib/logs.spec.ts
Adds LANGUAGE, IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, and NO_IMAGE finish reasons to the CONTENT_FILTER mapping for Google providers, with corresponding test cases asserting the new mappings.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Straightforward mapping additions with consistent pattern across both production and test files
  • No complex logic or control-flow changes introduced
  • Verify that all new finish-reason cases are appropriately matched to CONTENT_FILTER classification

Possibly related PRs

Suggested labels

auto-merge

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: mapping Google image safety finish reasons to content filters, which is the primary focus of the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch steebchen/image-safety-filter

📜 Recent review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ac12fc9 and d729fdb.

📒 Files selected for processing (2)
  • apps/gateway/src/lib/logs.spec.ts (1 hunks)
  • apps/gateway/src/lib/logs.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (8)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx}: Never use any or as any unless absolutely necessary in TypeScript code
For database reads: Use db().query.<table>.findMany() or db().query.<table>.findFirst()

Files:

  • apps/gateway/src/lib/logs.ts
  • apps/gateway/src/lib/logs.spec.ts
**/*.{ts,tsx,js,jsx,json,md}

📄 CodeRabbit inference engine (CLAUDE.md)

Always use tabs for indentation

Files:

  • apps/gateway/src/lib/logs.ts
  • apps/gateway/src/lib/logs.spec.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx,js,jsx}: Always use top-level import, never use require or dynamic imports
No unnecessary code comments

Files:

  • apps/gateway/src/lib/logs.ts
  • apps/gateway/src/lib/logs.spec.ts
apps/{gateway,api}/**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

Use Hono framework with Zod validation and OpenAPI documentation for backend APIs

Files:

  • apps/gateway/src/lib/logs.ts
  • apps/gateway/src/lib/logs.spec.ts
**/*.{js,ts,tsx,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

Always use top-level import, never use require or dynamic imports

Files:

  • apps/gateway/src/lib/logs.ts
  • apps/gateway/src/lib/logs.spec.ts
{apps/api,apps/gateway,packages/db}/**/*.ts

📄 CodeRabbit inference engine (AGENTS.md)

{apps/api,apps/gateway,packages/db}/**/*.ts: Use Drizzle ORM with latest object syntax for database operations
For database reads: Use db().query.<table>.findMany() or db().query.<table>.findFirst()

Files:

  • apps/gateway/src/lib/logs.ts
  • apps/gateway/src/lib/logs.spec.ts
apps/{gateway,api}/src/**/*.ts

📄 CodeRabbit inference engine (AGENTS.md)

apps/{gateway,api}/src/**/*.ts: Use Hono for backend framework in Gateway and API services
Use Zod schemas for validation in Hono services

Files:

  • apps/gateway/src/lib/logs.ts
  • apps/gateway/src/lib/logs.spec.ts
**/*.spec.ts

📄 CodeRabbit inference engine (CLAUDE.md)

Unit tests should use *.spec.ts file naming convention

Files:

  • apps/gateway/src/lib/logs.spec.ts
🧬 Code graph analysis (1)
apps/gateway/src/lib/logs.spec.ts (2)
apps/gateway/src/lib/logs.ts (1)
  • getUnifiedFinishReason (11-87)
packages/db/src/schema.ts (2)
  • UnifiedFinishReason (20-30)
  • UnifiedFinishReason (32-33)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: e2e-shards (3)
  • GitHub Check: e2e-shards (4)
  • GitHub Check: e2e-shards (1)
  • GitHub Check: e2e-shards (5)
  • GitHub Check: e2e-shards (2)
  • GitHub Check: build / run
  • GitHub Check: generate / run
  • GitHub Check: test / run
  • GitHub Check: lint / run
  • GitHub Check: autofix
🔇 Additional comments (2)
apps/gateway/src/lib/logs.spec.ts (1)

54-65: LGTM! Comprehensive test coverage for new finish reasons.

The test cases properly verify that all four new Google AI Studio finish reasons (LANGUAGE, IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, NO_IMAGE) correctly map to CONTENT_FILTER. The tests follow the established pattern and provide complete coverage.

apps/gateway/src/lib/logs.ts (1)

61-65: Mapping scope incomplete in review assessment.

The code maps nine finish reasons to CONTENT_FILTER (SAFETY, PROHIBITED_CONTENT, RECITATION, BLOCKLIST, SPII, LANGUAGE, IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, NO_IMAGE), but the review only mentions four. Verify that all finish reasons—particularly PROHIBITED_CONTENT, BLOCKLIST, and SPII—are legitimate Gemini API values and appropriate for content filter categorization.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the handling of Google's image-related finish reasons by adding four new finish reason mappings (IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, LANGUAGE, and NO_IMAGE) to the content filter classification for both google-ai-studio and google-vertex providers. This prevents these finish reasons from being incorrectly logged as "Unknown finish reason encountered" errors.

  • Added four new Google finish reasons to the content filter mapping
  • Added corresponding unit tests for the new finish reason mappings with google-ai-studio provider

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
apps/gateway/src/lib/logs.ts Added four new Google finish reasons (IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, LANGUAGE, NO_IMAGE) to the content filter mapping in the google-ai-studio/google-vertex case
apps/gateway/src/lib/logs.spec.ts Added unit tests for the four new finish reasons using google-ai-studio provider

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +54 to +65
expect(getUnifiedFinishReason("LANGUAGE", "google-ai-studio")).toBe(
UnifiedFinishReason.CONTENT_FILTER,
);
expect(getUnifiedFinishReason("IMAGE_SAFETY", "google-ai-studio")).toBe(
UnifiedFinishReason.CONTENT_FILTER,
);
expect(
getUnifiedFinishReason("IMAGE_PROHIBITED_CONTENT", "google-ai-studio"),
).toBe(UnifiedFinishReason.CONTENT_FILTER);
expect(getUnifiedFinishReason("NO_IMAGE", "google-ai-studio")).toBe(
UnifiedFinishReason.CONTENT_FILTER,
);

Copilot AI Dec 18, 2025

Copy link

Choose a reason for hiding this comment

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

The new finish reason tests only cover the "google-ai-studio" provider, but the implementation in logs.ts applies these finish reasons to both "google-ai-studio" and "google-vertex" providers (lines 47-48 use a shared case statement). Test coverage should be added for the "google-vertex" provider to ensure these finish reasons work correctly for both Google provider types.

Copilot uses AI. Check for mistakes.
@steebchen
steebchen added this pull request to the merge queue Dec 18, 2025
Merged via the queue into main with commit dd68447 Dec 18, 2025
19 of 20 checks passed
@steebchen
steebchen deleted the steebchen/image-safety-filter branch December 18, 2025 19:03
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