Skip to content

fix: be lenient for anthropic parsing - #157

Merged
thushan merged 3 commits into
mainfrom
fix/issue-154-lenient-anthropic-parsing
Jun 4, 2026
Merged

thushan merged 3 commits into
mainfrom
fix/issue-154-lenient-anthropic-parsing

Conversation

@thushan

@thushan thushan commented Jun 4, 2026 •

Copy link
Copy Markdown
Owner

Addresses #154 and how newer fields cause issues for the Anthropic transformer.

This PR makes it more lenient and accepting of unknown fields in the Anthropic Translator and proxy.

Summary by CodeRabbit

Release Notes

  • Improvements

    • Olla now tolerates unrecognised and experimental Anthropic request fields, ignoring them during translation and forwarding them unchanged in passthrough mode to backends with native Anthropic support.
  • Tests

    • Added regression test coverage for unknown field handling and updated security tests to reflect the new lenient parsing behaviour.

@coderabbitai

coderabbitai Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The PR modifies the Anthropic translator to tolerate unknown or experimental request fields instead of rejecting them. The JSON decoder now permits unlisted fields, security tests confirm tolerance behaviour and validate error boundaries, and documentation describes the new handling for users.

Changes

Lenient Anthropic Request Fields

Layer / File(s) Summary
Lenient JSON decoder implementation
internal/adapter/translator/anthropic/request.go
JSON decoder removes DisallowUnknownFields() to permit unknown Anthropic request fields. Comments clarify that unknown fields are silently ignored during translation.
Security test updates for unknown field tolerance
internal/adapter/translator/anthropic/security_test.go
Unknown-field rejection test replaced with tolerance assertions. Combined validation scenarios now expect parameter-validation errors rather than JSON-parsing failures when unknown fields and invalid parameters coexist.
Feature regression test and user documentation
internal/adapter/translator/anthropic/extended_features_test.go, docs/content/integrations/api-translation/anthropic.md
New regression test verifies context_management field is accepted and excluded from translated OpenAI requests. Documentation explains unknown/experimental fields are ignored during translation and forwarded unchanged in passthrough mode.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

documentation, enhancement

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: making the Anthropic parser more lenient by accepting unknown fields instead of rejecting them.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-154-lenient-anthropic-parsing

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.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/adapter/translator/anthropic/security_test.go`:
- Around line 414-415: The failing assertion is too broad (assert.Contains(t,
err.Error(), "invalid request")); tighten it to assert the specific validation
failure for the temperature parameter by checking the error text contains the
temperature validation path or message (e.g., include "temperature" or the full
validation phrase emitted by Validate()) so replace the broad check with an
assertion like assert.Contains(t, err.Error(), "temperature") or
assert.Contains(t, err.Error(), "request.temperature") to ensure the test fails
only when temperature bounds regress; refer to the local error variable err and
the Validate() temperature check in this test to locate where to update the
assertion.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 99c435fe-6dc3-426d-aa36-08b76db85490

📥 Commits

Reviewing files that changed from the base of the PR and between fd57b87 and aa0f4df.

📒 Files selected for processing (4)
  • docs/content/integrations/api-translation/anthropic.md
  • internal/adapter/translator/anthropic/extended_features_test.go
  • internal/adapter/translator/anthropic/request.go
  • internal/adapter/translator/anthropic/security_test.go

Comment on lines +414 to +415
// Unknown field is silently ignored; rejection comes from Validate() on temperature.
assert.Contains(t, err.Error(), "invalid request")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Tighten the failure assertion to the actual invalid parameter path.

Line 415 only asserting "invalid request" is too broad and can mask regressions unrelated to temperature bounds. Assert the specific validation reason as well.

Suggested test assertion tightening
-		assert.Contains(t, err.Error(), "invalid request")
+		assert.Contains(t, err.Error(), "invalid request")
+		assert.Contains(t, err.Error(), "temperature must be between 0 and 2")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Unknown field is silently ignored; rejection comes from Validate() on temperature.
assert.Contains(t, err.Error(), "invalid request")
// Unknown field is silently ignored; rejection comes from Validate() on temperature.
assert.Contains(t, err.Error(), "invalid request")
assert.Contains(t, err.Error(), "temperature must be between 0 and 2")
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/adapter/translator/anthropic/security_test.go` around lines 414 -
415, The failing assertion is too broad (assert.Contains(t, err.Error(),
"invalid request")); tighten it to assert the specific validation failure for
the temperature parameter by checking the error text contains the temperature
validation path or message (e.g., include "temperature" or the full validation
phrase emitted by Validate()) so replace the broad check with an assertion like
assert.Contains(t, err.Error(), "temperature") or assert.Contains(t,
err.Error(), "request.temperature") to ensure the test fails only when
temperature bounds regress; refer to the local error variable err and the
Validate() temperature check in this test to locate where to update the
assertion.

@thushan
thushan merged commit 5d98605 into main Jun 4, 2026
7 checks passed
@thushan
thushan deleted the fix/issue-154-lenient-anthropic-parsing branch June 4, 2026 06:56
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.

1 participant