fix: Gemini HARM_CATEGORY_JAILBREAK and Anthropic tool_result blocks#1867
fix: Gemini HARM_CATEGORY_JAILBREAK and Anthropic tool_result blocks#1867
Conversation
Combines critical fixes from PR #1863 and PR #1832: 1. Handle missing HARM_CATEGORY_JAILBREAK attribute in google.genai - Use hasattr check for compatibility across google.genai versions - HARM_CATEGORY_JAILBREAK is Vertex AI only, not in google.genai API - Fixes 400 errors with Gemini API safety settings 2. Ensure tool_result blocks for all Anthropic tool_use responses - Simplifies overly restrictive condition in reask_anthropic_tools - Always creates tool_result block when tool_use is present - Fixes 400 error during validation retries (#1829) Co-authored-by: DaveOkpare <DaveOkpare@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
|
Claude finished @jxnl's task —— View job Comprehensive PR Review ✅PR #1867: fix: Gemini HARM_CATEGORY_JAILBREAK and Anthropic tool_result blocks Overall AssessmentThis PR addresses two critical compatibility issues that were causing 400 errors in production. The fixes are well-implemented, focused, and include appropriate test coverage. ✅ Fix 1: Gemini HARM_CATEGORY_JAILBREAK HandlingProblem Addressed: The Solution Review:
Code Quality: ⭐⭐⭐⭐⭐ # Before: Hard-coded exclusion
if c != HarmCategory.HARM_CATEGORY_UNSPECIFIED
# After: Dynamic compatibility check
excluded_categories = {HarmCategory.HARM_CATEGORY_UNSPECIFIED}
if hasattr(HarmCategory, 'HARM_CATEGORY_JAILBREAK'):
excluded_categories.add(HarmCategory.HARM_CATEGORY_JAILBREAK)✅ Fix 2: Anthropic tool_result BlocksProblem Addressed: Complex validation logic in Solution Review:
Code Quality: ⭐⭐⭐⭐⭐ # Before: Complex, fragile condition
if (content.type == "tool_use"
and isinstance(exception, (ValidationError, InstructorValidationError))
and content.name == exception.title):
# After: Simple, reliable condition
if content.type == "tool_use":✅ Test Coverage AssessmentStrengths:
Missing Tests:
🔒 Security AssessmentNo security concerns identified:
⚡ Performance ImpactPositive performance characteristics:
🏆 Code Quality & Best PracticesExcellent adherence to best practices:
📝 Minor Suggestions
✅ Final Recommendation: APPROVEThis PR successfully addresses two critical production issues with:
The fixes are well-scoped, defensive in nature, and directly address the root causes of the 400 errors. Ready for merge. |
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed everything up to 80970be in 1 minute and 7 seconds. Click for details.
- Reviewed
76lines of code in3files - Skipped
0files when reviewing. - Skipped posting
3draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. instructor/providers/anthropic/utils.py:159
- Draft comment:
Simplified tool_use check now drops exception-type and title matching. Ensure this broader handling is intentional. - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =0%<= threshold85%The comment is asking the PR author to ensure that the broader handling of the tool_use check is intentional. This falls under the rule of not asking the author to confirm their intention or ensure behavior is intended. Therefore, this comment should be removed.
2. instructor/providers/gemini/utils.py:252
- Draft comment:
Using hasattr to conditionally add HARM_CATEGORY_JAILBREAK ensures compatibility. Consider centralizing this exclusion logic if it’s reused. - Reason this comment was not posted:
Comment looked like it was already resolved.
3. tests/llm/test_genai/test_utils.py:38
- Draft comment:
Test logic duplicates the excluded_categories computation. Consider factoring into a helper for DRY, though tests remain clear. - Reason this comment was not posted:
Confidence changes required:50%<= threshold85%None
Workflow ID: wflow_pWtDYANCofCZqMcO
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
instructor | 80970be | Commit Preview URL Branch Preview URL |
Oct 27 2025, 06:46 PM |
Summary
This PR combines critical fixes from PR #1863 and PR #1832.
Changes
1. Handle missing HARM_CATEGORY_JAILBREAK in google.genai
Problem: The HARM_CATEGORY_JAILBREAK category is only available in Vertex AI, not in the google.genai API.
Solution:
Files: instructor/providers/gemini/utils.py, tests/llm/test_genai/test_utils.py
2. Ensure tool_result blocks for all Anthropic tool_use responses
Problem: When validation fails in ANTHROPIC_TOOLS mode, retry mechanism creates malformed messages.
Solution:
Files: instructor/providers/anthropic/utils.py
Testing
Run tests:
Credits
Co-authored-by: DaveOkpare DaveOkpare@users.noreply.github.com
Co-authored-by: Claude noreply@anthropic.com
Important
Fixes handling of
HARM_CATEGORY_JAILBREAKingoogle.genaiand ensurestool_resultblocks for Anthropictool_useresponses.HARM_CATEGORY_JAILBREAKhandling inupdate_genai_kwargs()inutils.pyforgoogle.genai.tool_resultblocks are created for alltool_useresponses inreask_anthropic_tools()inutils.py.test_utils.pyto verifyHARM_CATEGORY_JAILBREAKexclusion andtool_resultblock creation.reask_anthropic_tools()to remove exception type and title matching.This description was created by
for 80970be. You can customize this summary. It will automatically update as commits are pushed.