Problem/Motivation

During production use of the Milvus VDB Provider with AI Search 2.0.x and OpenAI embeddings, two bugs were discovered that impact reliability and developer experience:

Issue 1: Insufficient Null Safety in Response Processing

File: `src/MilvusV2.php` - `ensureEntityIdInResponse()` method (line 435)

Problem: The method does not properly handle edge cases when the API response is empty or malformed. The original code assumes `$response['data']` exists and
is a valid array before processing, which can cause errors when:
- The Milvus API returns an empty result set
- The response structure is malformed
- Individual data items are not arrays

Impact: Can cause PHP errors/warnings when processing edge case responses, particularly:
- Empty search results
- Malformed API responses
- Non-array data items in the response

Error Example:

 Warning: foreach() argument must be of type array|object, null given
 TypeError: Cannot access offset of type string on array

Issue 2: Incorrect Method Call in Warning Message

File: `src/Plugin/VdbProvider/MilvusProvider.php` - Line 661

Problem: When displaying a warning about unsupported negative operators on multiple fields, the code incorrectly calls:

'@name' => $this->getClient()->getPluginId(),

This should be:

 '@name' => $this->getPluginId(),

Impact:
- May cause method call errors depending on the client implementation
- The warning message may not display correctly or at all
- Reduces developer experience when debugging query issues

Proposed resolution

Fix 1: Improve Null Safety in ensureEntityIdInResponse()

File: `src/MilvusV2.php`

Replace the method body (starting around line 435) with proper null checks:

protected function ensureEntityIdInResponse(array $response): array {
   if (
     !isset($response['data']) ||
     !is_array($response['data']) ||
     empty($response['data'])
   ) {
     // No results is a valid response.
     return $response;
   }

   foreach ($response['data'] as &$data) {
     if (!is_array($data)) {
       continue;
     }

     // If Drupal Entity ID is set, it means the entity field is a meta data
     // field and the 'outputFields' parameter would return it completely for all.
     if (!empty($data['drupal_entity_id'])) {
       break;
     }

     // Derive the 'drupal_entity_id' from the 'drupal_long_id' field since
     // the 'outputFields' parameter fails to return non-meta fields.
     if (!empty($data['drupal_long_id'])) {
       $long_id = $data['drupal_long_id'];
       $last_colon_position = strrpos($long_id, ':');
       if ($last_colon_position !== FALSE) {
         $data['drupal_entity_id'] = substr($long_id, 0, $last_colon_position);
       }
     }
   }
   return $response;
 }

Changes:
1. Add explicit null/isset check for `$response['data']`
2. Add early return when data is empty or invalid
3. Add type check inside foreach to skip non-array items
4. Maintain all existing functionality while improving robustness

Fix 2: Correct Method Call in Warning Message

File: `src/Plugin/VdbProvider/MilvusProvider.php`

Change line 661 from:

 '@name' => $this->getClient()->getPluginId(),

To:

 '@name' => $this->getPluginId(),

Steps to reproduce

For Issue 1:
1. Set up Milvus VDB Provider with AI Search
2. Perform a search query that returns no results
3. Or, simulate a malformed API response
4. Observe: PHP warnings/errors about invalid array access

For Issue 2:
1. Set up Milvus VDB Provider with Search API
2. Configure a facet or filter with negative operator on a multiple-value field
3. Observe: Warning message may fail to display or show incorrect plugin name

Testing

Test Environment

:
- Drupal 10.6.1
- AI module 2.0.x
- AI Search 2.0.0-alpha1
- Milvus VDB Provider (current 2.0.x branch)
- OpenAI text-embedding-3-large model
- 7000+ products indexed

Test Results:

  • Empty search results no longer cause errors
  • Malformed responses handled gracefully
  • Warning messages display correctly with the proper plugin name
  • All existing functionality preserved
  • No performance impact

Manual Testing Performed:

1. Empty Results Test: Searched for nonsense keywords - returned empty results without errors
2. Normal Operation: Standard searches continue to work correctly
3. Warning Display Test: Verified warning messages show correct plugin name
4. Edge Cases: Tested various malformed response scenarios

API changes

None. These are internal bug fixes that maintain backward compatibility.

Data model changes

None.

Release Notes Snippet

> **Bug fixes for Milvus VDB Provider:**
> - Fixed null safety issues in response processing that could cause errors with empty search results
> - Corrected method call in warning message display for unsupported operators
> - Improved error handling for edge case API responses

Related Issues

These fixes were discovered during testing related to:
- AI Search 2.0.x compatibility
- Hybrid search implementation with Solr
- Production use with OpenAI embeddings

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mbatterton created an issue. See original summary.

scott_euser’s picture

Status: Active » Closed (won't fix)

Sorry unfortunately this is the same issue as your AI Search one; cannot accept a merge request that is covering many issues at once. Its too difficult to understand steps to reproduce, test, and ensure the fix is appropriate (particularly since as you noted, its fully AI generated). Please feel free to reopen separate issues clearly following the issue summary guidelines, and with confirmation that you have both reviewed the AI generated code and tested manually yourself.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.