fix(anthropic): preserve file id document sources - #4832
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR fixes Anthropic document block conversion to preserve OpenAI-compatible uploaded file references ( ChangesAnthropic file source fix
Estimated code review effort: 1 (Trivial) | ~5 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" 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. Comment |
Preserves uploaded file references when OpenAI-compatible chat file blocks are converted into Anthropic document blocks. Affected packages: - core/providers/anthropic/ - document block conversion and regression coverage - core/changelog.md - user-facing changelog entry
3aa12d8 to
8158040
Compare
Summary
Fixes Anthropic document conversion when Bifrost receives an OpenAI-compatible chat completion request that references an uploaded Anthropic file by
file_id.When This Happens
When the routed model is Anthropic and the message contains a file content block like below on OpenAI-compatible routes such as
/litellm/v1/chat/completionsor/v1/chat/completions, Bifrost converts that OpenAI-compatible file block into an Anthropic document block. Before this fix, uploadedfile_idreferences were not copied into the Anthropic document source object, andsource.typewas not set tofile, causing Anthropic to reject the request.OpenAI-compatible input:
{ "type": "file", "file": { "file_id": "file_abc123", "filename": "tiny.pdf" } }The failure happens while converting the OpenAI-compatible chat request into Anthropic Messages API content blocks. Before the fix, Bifrost produced an Anthropic document block with an initialized but invalid source object:
{ "type": "document", "title": "tiny.pdf", "source": { "type": "" } }Anthropic rejects this because
document.source.typemust be one of its supported source tags, for examplefile,base64,url, ortext. After the fix, Bifrost preserves the uploaded file reference in Anthropic's expected file-source shape:{ "type": "document", "title": "tiny.pdf", "source": { "type": "file", "file_id": "file_abc123" } }Reproduction
A self-contained reproduction case is documented here:
talismanai/bifrost-issue: anthropic-file-id-openai-route
That case contains the run instructions, a self-contained uv sample script, and the Bifrost configuration used for local validation. The script uploads a tiny PDF to
POST /v1/files?provider=anthropic, callsPOST /litellm/v1/chat/completionswith the uploadedfile_id, prints the response, and deletes the uploaded file.Changes
ChatInputFile.FileIDto Anthropicsource: {type: "file", file_id: ...}.core/changelog.md.Type of change
Affected areas
How to test
go test ./providers/anthropic ./providers/openai go vet ./providers/anthropic git diff --checkFor end-to-end validation, run the external reproduction case linked above against a local Bifrost gateway configured with an Anthropic key and the
anthropic-beta: files-api-2025-04-14header.Screenshots/Recordings
N/A
Breaking changes
Related issues
N/A
Security considerations
No new auth, secret, or data exposure paths. This only preserves an existing uploaded file ID in the downstream Anthropic request body. The external repro reads credentials from environment variables only.
Checklist