Skip to content

Conversation

@eun2ce
Copy link

@eun2ce eun2ce commented Nov 27, 2025

Related issue

Fixes #1143

What does this PR do?

  • Ensure that custom_headers passed to meilisearch.Client are propagated to Index instances and task-related HTTP calls.
  • Store custom_headers on the Client (self._custom_headers) so that it can be reused when instantiating sub-clients.
  • Update TaskHandler to accept custom_headers and pass them to its internal HttpRequests instance.
  • Update Index:
    • __init__ now accepts an optional custom_headers parameter and uses it for both HttpRequests and TaskHandler.
    • Index.create now accepts an optional custom_headers parameter and forwards it to HttpRequests.
  • Update Client:
    • Pass self._custom_headers when calling Index.create in create_index.
    • Pass self._custom_headers when creating Index instances in get_indexes, get_index, and index.
  • Add a test (test_index_inherits_custom_headers) to verify that Index.http.headers includes the custom headers when they are provided to the Client.

PR checklist

Please check if your PR fulfills the following requirements:

  • Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
  • Have you read the contributing guidelines?
  • Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!

Summary by CodeRabbit

  • Bug Fixes

    • Custom headers now consistently propagate through all client operations, including index creation, retrieval, and task handling, ensuring uniform header application across the system.
  • Tests

    • Added test coverage validating custom header propagation from client to index operations.

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 27, 2025

Walkthrough

Custom headers propagation is implemented throughout the client hierarchy. The Client stores custom headers and passes them to HttpRequests, TaskHandler, and Index instances. Index also receives and propagates custom headers to its internal HttpRequests and TaskHandler. A test verifies Index inherits Client custom headers.

Changes

Cohort / File(s) Change Summary
Client-level custom headers storage and propagation
meilisearch/client.py
Client constructor now stores custom_headers as instance variable _custom_headers and passes it to TaskHandler, Index.create, and all index retrieval methods (get_indexes, get_index, index).
Index initialization with custom headers
meilisearch/index.py
Index.init accepts optional custom_headers parameter and propagates it to HttpRequests and TaskHandler instances. Index.create static method now accepts and forwards custom_headers to HttpRequests for POST requests.
TaskHandler custom headers support
meilisearch/task.py
TaskHandler.init accepts optional custom_headers parameter and passes it to HttpRequests initialization. Adds Mapping import for type annotation.
Custom headers inheritance test
tests/client/test_client.py
New test_index_inherits_custom_headers() verifies that Index obtained from a Client with custom_headers correctly includes those headers in HTTP requests.

Sequence Diagram

sequenceDiagram
    participant User
    participant Client
    participant Index
    participant TaskHandler
    participant HttpRequests

    User->>Client: __init__(url, api_key, custom_headers=X)
    Client->>Client: Store custom_headers as _custom_headers
    
    User->>Client: index(uid="movies")
    Client->>Index: __init__(config, uid, custom_headers=_custom_headers)
    Index->>HttpRequests: __init__(config, custom_headers)
    HttpRequests->>HttpRequests: Store custom_headers
    Index->>TaskHandler: __init__(config, custom_headers)
    TaskHandler->>HttpRequests: __init__(config, custom_headers)
    
    User->>Index: Operations (get, search, etc.)
    Index->>HttpRequests: Request with custom_headers included
    HttpRequests-->>Index: Response
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Changes follow a consistent, repetitive pattern of threading custom_headers through constructors
  • Logic is straightforward—storing and forwarding parameters without complex branching
  • Test case is straightforward validation of the propagation chain
  • All modifications are localized to initialization paths with clear intent

Poem

🐰 Headers hop through the client's warren,
From Client to Index, they don't tarry—
Custom threads woven, thread by thread,
Through HttpRequests, TaskHandler spread,
Now auth flows where it's meant to go,
No more headers left in the snow! 🎀

Pre-merge checks and finishing touches

✅ Passed checks (5 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 describes the main change: propagating custom_headers to Index instances, which directly addresses the primary objective of the PR.
Linked Issues check ✅ Passed The PR fully addresses all coding requirements from issue #1143: custom_headers are now propagated to Index instances through Client, TaskHandler, and HttpRequests, preventing authentication failures.
Out of Scope Changes check ✅ Passed All changes are directly related to propagating custom_headers through the client hierarchy; no unrelated or out-of-scope modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 86.67% 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

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
tests/client/test_client.py (1)

63-70: LGTM!

Good test coverage for the primary use case. The test correctly verifies that custom headers propagate from Client to Index's HttpRequests instance.

Consider adding an assertion for index.task_handler.http.headers to verify headers also propagate to the TaskHandler within the Index:

    # Also verify TaskHandler gets the custom headers
    assert index.task_handler.http.headers.items() >= custom_headers.items()
meilisearch/index.py (2)

62-87: LGTM!

The custom_headers parameter is correctly added to __init__ and properly propagated to both HttpRequests and TaskHandler. This ensures all HTTP calls from an Index instance will include the custom headers.

The docstring (lines 71-80) does not document the new custom_headers parameter. Consider adding:

         primary_key:
             Primary-key of the index.
+        custom_headers:
+            Custom headers to add when sending data to Meilisearch.
         """

178-210: LGTM!

The static create method correctly accepts custom_headers and propagates it to the HttpRequests instance used for the POST request. This ensures index creation respects custom headers.

The docstring for create (lines 185-204) does not document the new custom_headers parameter. Consider adding documentation for completeness.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 23debbd and 3f85693.

📒 Files selected for processing (4)
  • meilisearch/client.py (5 hunks)
  • meilisearch/index.py (4 hunks)
  • meilisearch/task.py (2 hunks)
  • tests/client/test_client.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
meilisearch/task.py (2)
meilisearch/config.py (1)
  • Config (6-72)
meilisearch/_httprequests.py (1)
  • HttpRequests (19-227)
meilisearch/client.py (3)
meilisearch/_httprequests.py (1)
  • HttpRequests (19-227)
meilisearch/task.py (1)
  • TaskHandler (14-212)
meilisearch/index.py (2)
  • Index (54-2384)
  • create (179-210)
🔇 Additional comments (6)
meilisearch/task.py (1)

22-28: LGTM!

The custom_headers parameter is properly added with correct typing (Optional[Mapping[str, str]]) and is correctly propagated to the HttpRequests instance. The default value of None maintains backward compatibility.

meilisearch/client.py (5)

77-82: LGTM!

Storing custom_headers as self._custom_headers is the correct approach for propagating headers to sub-clients. The underscore prefix appropriately indicates this is for internal use. Both HttpRequests and TaskHandler correctly receive the custom headers.


105-105: LGTM!

Custom headers are correctly propagated to Index.create, ensuring the index creation HTTP request includes the custom headers.


152-162: LGTM!

Custom headers are correctly propagated to each Index instance created from the response in get_indexes.


208-208: LGTM!

Custom headers are correctly propagated to the Index instance in get_index, ensuring subsequent operations on the returned Index will include the headers.


245-247: LGTM!

Custom headers are correctly propagated to the Index instance in the index method. This completes the header propagation for all index-related client methods.

@Strift Strift added the enhancement New feature or request label Dec 1, 2025
Copy link
Contributor

@Strift Strift left a comment

Choose a reason for hiding this comment

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

Hello @eun2ce and thanks for your contribution 🙌

Let's get this merged!

@Strift Strift added this pull request to the merge queue Dec 1, 2025
Merged via the queue into meilisearch:main with commit 6e04189 Dec 1, 2025
11 checks passed
@Strift Strift added bug Something isn't working and removed enhancement New feature or request labels Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

custom_headers are not properly propagated to Index instances.

2 participants