Skip to content

Conversation

@jawad-khan
Copy link
Contributor

@jawad-khan jawad-khan commented Dec 3, 2025

Related issue

Fixes #1168

What does this PR do?

Update Index APIs to Support customMetadata

This PR updates the index-related APIs in the Python SDK to align with recent Meilisearch server changes and adds support for the new customMetadata field.

Key Updates

  • Added customMetadata support across index creation and update methods.

  • Synced request/response structures with the latest Meilisearch index API.

  • ...

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

  • New Features

    • Tasks now support custom metadata. Document operations (add, update, delete, single and batched) accept an optional metadata parameter that is attached to the created task and returned in task info for improved tracking.
  • Tests

    • Tests updated to verify metadata is propagated and exposed on returned task information after operations.

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

Copilot AI review requested due to automatic review settings December 3, 2025 12:57
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 3, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds an optional metadata: Optional[str] parameter to document add/update/delete APIs, threads it into internal calls and URL construction as customMetadata, and exposes it on returned Task objects via a new customMetadata field. Tests updated to assert metadata propagation.

Changes

Cohort / File(s) Summary
Index API methods
meilisearch/index.py
Added metadata: Optional[str] = None to public document methods (add, update, delete, batch and raw/CSV/NDJSON variants). Propagates through helpers and into _build_url so customMetadata=<value> is appended to request URLs when provided.
Task model
meilisearch/models/task.py
Added customMetadata: Optional[str] = None field to the Task model to surface metadata returned by the server.
Tests
tests/index/test_index_document_meilisearch.py
Updated/added tests to pass metadata to document operations and assert returned TaskInfo contains the provided customMetadata.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20–30 minutes

  • Inspect all changed public method signatures in meilisearch/index.py for consistent ordering and defaults.
  • Verify _build_url appends customMetadata only when metadata is non-empty and URL encoding is correct.
  • Check batch loop paths ensure metadata is forwarded for every per-batch request.
  • Confirm Task deserialization/creation populates customMetadata from API responses and tests validate both presence and absence cases.

Possibly related PRs

  • Add sharding support #1164 — Modifies Task model fields (adds network-related field); related because both PRs change meilisearch/models/task.py.

Suggested labels

enhancement

Suggested reviewers

  • Strift

Poem

🐰 I hopped through code, a tiny tweak in tow,
I tucked a note where tasks would go.
A string to carry what devs might say,
Custom metadata to brighten the day. ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 73.68% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ 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 'feat: Added customMetadata in index apis' directly and accurately summarizes the main change—adding custom metadata support to index API methods.
Linked Issues check ✅ Passed All requirements from issue #1168 are met: metadata parameter added to document add/update/delete methods, propagated through API calls, and test cases added to verify customMetadata functionality.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing custom metadata support across index operations as specified in issue #1168; no unrelated modifications detected.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a03eff0 and 690ec39.

📒 Files selected for processing (1)
  • tests/index/test_index_document_meilisearch.py (15 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/index/test_index_document_meilisearch.py (2)
meilisearch/index.py (12)
  • add_documents (479-514)
  • update (108-152)
  • add_documents_in_batches (516-562)
  • add_documents_json (564-599)
  • update_documents (714-749)
  • update_documents_json (783-818)
  • delete_document (948-975)
  • delete_all_documents (1032-1055)
  • add_documents_csv (601-634)
  • update_documents_csv (820-853)
  • add_documents_ndjson (636-666)
  • update_documents_ndjson (751-781)
meilisearch/models/task.py (1)
  • TaskInfo (79-108)
🔇 Additional comments (5)
tests/index/test_index_document_meilisearch.py (5)

34-40: Excellent test coverage for add_documents operations.

The tests correctly verify that the metadata parameter propagates to task.customMetadata for both single and batch add operations. The pattern of passing metadata="Test metadata" and asserting the value after wait_for_task is correct and consistent.

Also applies to: 65-75


112-120: Good integration of metadata with custom serializer tests.

These tests effectively verify that metadata works correctly alongside custom serialization (JSON, raw formats) for both add and update operations. The combination ensures that the metadata feature doesn't interfere with other functionality.

Also applies to: 129-140, 149-155, 177-185, 194-205


393-398: Update operations properly tested with metadata.

Both single and batch update operations correctly verify metadata propagation. The tests ensure that the feature works across different update scenarios.

Also applies to: 423-432


440-445: Comprehensive delete operation coverage with metadata.

All delete variants are tested: single document deletion (by ID), batch deletion (by ID list), filtered deletion, and delete-all. The previous assignment bug on line 475 has been correctly fixed to use assertion (assert task.customMetadata == "Test metadata").

Also applies to: 455-460, 473-476, 486-490


499-503: Format-specific operations thoroughly tested.

CSV, JSON, and NDJSON format handlers all properly verify metadata propagation for both add and update operations, ensuring comprehensive coverage across all supported document formats.

Also applies to: 525-530, 551-556, 563-567, 575-579, 587-592


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

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for the customMetadata field to index-related APIs in the Python SDK, aligning with recent Meilisearch server changes. The implementation adds an optional metadata parameter to all document manipulation methods (add, update, delete) that gets passed to the Meilisearch server as a query parameter.

Key changes:

  • Added customMetadata field to the Task model to capture metadata returned by the server
  • Updated all document methods in the Index class to accept an optional metadata parameter
  • Enhanced test coverage by adding metadata assertions to document operation tests

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
meilisearch/models/task.py Added customMetadata field as Optional[str] to Task model
meilisearch/index.py Added metadata parameter to all document methods (add/update/delete variants) and updated URL building logic to include customMetadata as a query parameter
tests/index/test_index_document_meilisearch.py Updated tests to pass metadata parameter and assert that customMetadata is persisted on task objects

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

self,
documents: Sequence[Mapping[str, Any]],
batch_size: int = 1000,
primary_key: Optional[str] = None,
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

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

Inconsistent API design: In add_documents_in_batches, serializer is a keyword-only argument (line 521-522 uses *,), but in this method it's not keyword-only. For consistency, consider adding *, before serializer on line 906 to make it keyword-only, or update add_documents_in_batches to match this signature. The new metadata parameter should also be keyword-only to match the pattern in add_documents_in_batches.

Suggested change
primary_key: Optional[str] = None,
primary_key: Optional[str] = None,
*,

Copilot uses AI. Check for mistakes.
index.wait_for_task(response.task_uid)
response = index.delete_documents(filter="genre=action", metadata="Test metadata")
task = index.wait_for_task(response.task_uid)
task.customMetadata = "Test metadata"
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

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

This line should be an assertion (assert task.customMetadata == "Test metadata") instead of an assignment. Unlike all other test cases in this file, this line assigns a value instead of verifying it, which means the test isn't actually validating that the metadata was properly persisted.

Suggested change
task.customMetadata = "Test metadata"
assert task.customMetadata == "Test metadata"

Copilot uses AI. Check for mistakes.
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: 1

🧹 Nitpick comments (4)
meilisearch/index.py (4)

601-634: Inconsistent parameter style: metadata is positional here but keyword-only in similar methods.

In add_documents, add_documents_in_batches, and add_documents_json, the metadata parameter is keyword-only (after *). However, in add_documents_csv, it's a regular positional parameter.

For API consistency, consider making metadata keyword-only:

 def add_documents_csv(
     self,
     str_documents: bytes,
     primary_key: Optional[str] = None,
     csv_delimiter: Optional[str] = None,
+    *,
     metadata: Optional[str] = None,
 ) -> TaskInfo:

636-666: Same inconsistency: metadata should be keyword-only for consistency.

 def add_documents_ndjson(
     self,
     str_documents: bytes,
     primary_key: Optional[str] = None,
+    *,
     metadata: Optional[str] = None,
 ) -> TaskInfo:

751-781: Same inconsistency: metadata should be keyword-only.

 def update_documents_ndjson(
     self,
     str_documents: str,
     primary_key: Optional[str] = None,
+    *,
     metadata: Optional[str] = None,
 ) -> TaskInfo:

820-853: Same inconsistency: metadata should be keyword-only.

 def update_documents_csv(
     self,
     str_documents: str,
     primary_key: Optional[str] = None,
     csv_delimiter: Optional[str] = None,
+    *,
     metadata: Optional[str] = None,
 ) -> TaskInfo:
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5fdcb7b and a03eff0.

📒 Files selected for processing (3)
  • meilisearch/index.py (37 hunks)
  • meilisearch/models/task.py (1 hunks)
  • tests/index/test_index_document_meilisearch.py (15 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
tests/index/test_index_document_meilisearch.py (1)
meilisearch/index.py (14)
  • add_documents (479-514)
  • update (108-152)
  • add_documents_in_batches (516-562)
  • add_documents_json (564-599)
  • update_documents (714-749)
  • update_documents_json (783-818)
  • update_documents_in_batches (901-946)
  • delete_document (948-975)
  • delete_documents (978-1030)
  • delete_all_documents (1032-1055)
  • add_documents_csv (601-634)
  • update_documents_csv (820-853)
  • add_documents_ndjson (636-666)
  • update_documents_ndjson (751-781)
meilisearch/index.py (2)
meilisearch/_httprequests.py (2)
  • put (120-140)
  • post (98-108)
meilisearch/models/task.py (1)
  • TaskInfo (79-108)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Agent
🔇 Additional comments (31)
meilisearch/models/task.py (1)

25-25: LGTM - customMetadata field added to Task model.

The field correctly uses Optional[str] and follows the existing CamelBase naming convention for API compatibility.

tests/index/test_index_document_meilisearch.py (18)

31-40: LGTM - Test correctly verifies metadata propagation.


65-74: LGTM - Batch test correctly verifies metadata on each task.


112-120: LGTM - JSON serializer test with metadata verification.


129-140: LGTM - Raw documents test with metadata verification.


149-155: LGTM - Update documents test with metadata.


177-185: LGTM - Update documents JSON test with metadata.


194-205: LGTM - Update documents raw test with metadata.


393-398: LGTM - Update documents test with metadata.


423-432: LGTM - Update documents in batches test with metadata.


440-444: LGTM - Delete document test with metadata.


455-459: LGTM - Delete documents by ID test with metadata.


486-490: LGTM - Delete all documents test with metadata.


499-503: LGTM - Add documents CSV test with metadata.


525-530: LGTM - Update documents CSV test with metadata.


551-556: LGTM - Add documents JSON test with metadata.


563-567: LGTM - Update documents JSON test with metadata.


575-579: LGTM - Add documents NDJSON test with metadata.


587-592: LGTM - Update documents NDJSON test with metadata.

meilisearch/index.py (12)

479-514: LGTM - add_documents method correctly supports metadata parameter.

The metadata is properly threaded through to _build_url and documented in the docstring.


516-562: LGTM - add_documents_in_batches correctly propagates metadata to each batch.


564-599: LGTM - add_documents_json correctly propagates metadata.


668-712: LGTM - add_documents_raw correctly implements metadata as keyword-only.


714-749: LGTM - update_documents correctly implements metadata.


783-818: LGTM - update_documents_json correctly implements metadata as keyword-only.


855-899: LGTM - update_documents_raw correctly implements metadata as keyword-only.


901-946: LGTM - update_documents_in_batches correctly propagates metadata.

Note: The parameter style follows the existing pattern for this method (positional optional parameters), though it differs from add_documents_in_batches which uses keyword-only parameters. This is a pre-existing inconsistency.


948-975: LGTM - delete_document correctly implements metadata with URL encoding.


977-1030: LGTM - delete_documents correctly handles metadata in both the ids and filter branches.


1032-1055: LGTM - delete_all_documents correctly implements metadata.


2430-2445: LGTM - _build_url correctly centralizes the metadata query parameter construction.

Good approach to centralize the metadatacustomMetadata mapping in the URL builder.

@jawad-khan
Copy link
Contributor Author

@Strift Tests are failing because sharding require Enterprose addition now. Should we adjust test cases or somehow we can skip this check.

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.

[v1.26] Add tasks custom metadata

1 participant