Skip to content

fix(test): mock get_auth_header instead of get_api_key in anthropic file content test#24258

Merged
yuneng-jiang merged 1 commit intoBerriAI:mainfrom
joereyna:fix/anthropic-file-content-test-mock
Mar 20, 2026
Merged

fix(test): mock get_auth_header instead of get_api_key in anthropic file content test#24258
yuneng-jiang merged 1 commit intoBerriAI:mainfrom
joereyna:fix/anthropic-file-content-test-mock

Conversation

@joereyna
Copy link
Copy Markdown
Contributor

@joereyna joereyna commented Mar 20, 2026

Summary

Fixes a regression introduced by f415b72 which added get_auth_header to the handler. The test was mocking get_api_key but the handler now calls get_auth_header directly, causing the mock to be bypassed — resulting in a real HTTP request to Anthropic and a 400 instead of the expected ValueError.

Root Cause

  • Test added Dec 2025 mocking get_api_key
  • Mar 18 2026: f415b72b changed handler to call get_auth_header instead
  • Mock stopped working, real HTTP request made to api.anthropic.com, got 400

Fix

Mock get_auth_header directly since that's what the handler calls to gate on missing credentials.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Mar 20, 2026 11:13pm

Request Review

@codspeed-hq
Copy link
Copy Markdown
Contributor

codspeed-hq bot commented Mar 20, 2026

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing joereyna:fix/anthropic-file-content-test-mock (f0e0d98) with main (72c307d)

Open in CodSpeed

@yuneng-jiang yuneng-jiang self-requested a review March 20, 2026 23:10
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 20, 2026

Greptile Summary

This PR fixes a test regression in test_afile_content_missing_api_key caused by commit f415b72b, which changed AnthropicFilesHandler.afile_content to call get_auth_header instead of get_api_key for credential resolution. The old mock on get_api_key was silently bypassed, causing the test to make a real HTTP request to api.anthropic.com and receive a 400 instead of the expected ValueError.

Key changes:

  • Replaces patch.object(handler.anthropic_model_info, "get_api_key", return_value=None) with patch.object(handler.anthropic_model_info, "get_auth_header", return_value=None) in test_afile_content_missing_api_key, correctly intercepting the gating check in the handler.

Remaining concern:

  • A sibling test (test_afile_content_mixed_results, line 306) still carries a dead get_api_key mock that is also never exercised by the handler post-refactor. That test passes incidentally because the credential is supplied as an explicit argument, but the mock should be updated to patch get_auth_header for consistency and future-proofing.

Confidence Score: 4/5

  • This PR is safe to merge; the targeted fix is correct. A minor dead-mock cleanup is recommended but not blocking.
  • The one-line change is precise and correctly addresses the root cause. The handler's get_auth_header gating check is now properly mocked, ensuring the test no longer makes real outbound HTTP calls. A small residual issue — a dead get_api_key mock in a related test — does not affect correctness but leaves misleading test code.
  • tests/test_litellm/llms/anthropic/test_anthropic_files_and_batches.py — line 306 still has a dead get_api_key mock that should be updated.

Important Files Changed

Filename Overview
tests/test_litellm/llms/anthropic/test_anthropic_files_and_batches.py Fixes test_afile_content_missing_api_key to mock get_auth_header (what the handler now calls) instead of get_api_key. The targeted fix is correct, but a sibling test (test_afile_content_mixed_results) still carries a now-dead get_api_key mock at line 306 that should also be updated.

Sequence Diagram

sequenceDiagram
    participant Test
    participant Handler as afile_content
    participant ModelInfo as AnthropicModelInfo
    participant HTTP as httpx client

    Note over Test,HTTP: After fix — mock intercepts correctly
    Test->>Handler: afile_content(file_id, no credentials)
    Handler->>ModelInfo: get_auth_header() [mocked to return None]
    ModelInfo-->>Handler: None
    Handler-->>Test: raises ValueError ✅

    Note over Test,HTTP: Before fix — mock was bypassed
    Test->>Handler: afile_content(file_id, no credentials)
    Handler->>ModelInfo: get_auth_header() [NOT mocked, get_api_key was]
    ModelInfo-->>Handler: reads env vars
    Handler-->>HTTP: real outbound request to Anthropic — returns 400 ❌
Loading

Comments Outside Diff (1)

  1. tests/test_litellm/llms/anthropic/test_anthropic_files_and_batches.py, line 306 (link)

    P2 Dead mock left over from the same refactor

    This test still patches get_api_key on the anthropic_model_info instance, but after commit f415b72b the handler calls get_auth_header(...) instead, so this patch has no effect. The test still passes because the caller passes the credential explicitly as an argument, which flows through the real get_auth_header implementation and produces a valid header without any mocking.

    The dead mock is misleading: a future reader would reasonably believe it is what authorizes the HTTP call. To match the fix applied in test_afile_content_missing_api_key, this mock should be updated to patch get_auth_header with a non-None return value instead.

Last reviewed commit: "fix(test): mock get_..."

@yuneng-jiang yuneng-jiang enabled auto-merge March 20, 2026 23:11
@yuneng-jiang yuneng-jiang merged commit e6e3085 into BerriAI:main Mar 20, 2026
38 of 39 checks passed
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