Skip to content

Conversation

@younghosck
Copy link
Contributor

Description

Fixes ValidationError in StorageManager.address_request when storage operations return lists or edge case values (None, empty string, empty list). The issue occurs because StorageResponse.response_message requires a string, but lists were passed directly.

Problem

When sto_retrieve returns an empty list [], it causes a ValidationError since response_message expects a string. See issue #515.

Solution

  • Convert list results to JSON strings
  • Handle None, empty string, and empty list with meaningful messages
  • Ensure all result types are properly normalized to strings

Changes

  • aios/storage/storage.py: Updated address_request to handle all result types and edge cases

Copy link
Collaborator

@evison evison left a comment

Choose a reason for hiding this comment

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

Thank you for the pull request. Could you consider making the following revisions?

Syntax/indentation: In the diff the line after elif result is None: appears unindented; that would produce an IndentationError / SyntaxError. Make sure result_str = ... is indented under the elif.

Fragile empty-list handling: You convert lists to a string and then test result_str == "[]". That can be brittle (and non-JSON list formatting uses single quotes). Better to detect empty lists/tuples before converting.

Inconsistent serialization: You json.dumps dicts but fall back to str() for lists/tuples. Consider using json.dumps for lists/tuples too (with a try/except fallback if not JSON-serializable).

The elif result == "" check works but could mis-handle other falsy values (0, False). Keep it explicit as result == "" if that’s intended.

No handling for bytes: if result can be bytes, you may want to decode before comparing/serializing.

Suggested corrected implementations:
Import json at top.
Handle dict/list/tuple with json.dumps and explicit empty-list check before string conversion.
Fallback to str(result) if json.dumps fails.

Example corrected snippet:

import json
def address_request(self, agent_request):
    result = self.filesystem.address_request(agent_request)
    # Normalize result to string format for StorageResponse
    if isinstance(result, (dict, list, tuple)):
        try:
            result_str = json.dumps(result)
        except (TypeError, ValueError):
            result_str = str(result)
        # Empty list/tuple should show a meaningful message
        if isinstance(result, (list, tuple)) and len(result) == 0:
            result_str = "No documents found"
    elif result is None:
        result_str = "Operation completed with no result"
    elif result == "":
        result_str = "Operation completed with empty result"
    else:
        # Ensure we always return a string
        result_str = str(result)

    return StorageResponse(
        response_message=result_str,
        finished=True
    )

Extra recommendations:

Add unit tests verifying behavior for: dict, list (non-empty and empty), None, "", bytes, int, and custom objects.

Consider whether StorageResponse could accept other types (and do the conversion there) to centralize normalization.

If preserving JSON semantics is important, always use json.dumps for serializable objects so clients get consistent formatting.

@younghosck
Copy link
Contributor Author

Thank you for the detailed review!

I've understood all the points you mentioned:

  • Fixing the indentation issue
  • Improving empty list handling before string conversion
  • Using json.dumps consistently across dict/list/tuple with a fallback
  • Keeping explicit checks for empty strings
  • Adding bytes handling

I'll update the PR with these revisions and include the improved normalization logic you suggested.

Thanks again for the thorough feedback — very helpful!

Copy link
Collaborator

@evison evison left a comment

Choose a reason for hiding this comment

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

if isinstance(result, (list, tuple)) and len(result) == 0:

The above line will never execute since it's within the if isinstance(result, dict) condition. Need to change this line "if isinstance(result, dict):" to "if isinstance(result, (dict, list, tuple)):"

@aiosfoundation aiosfoundation merged commit 6312081 into agiresearch:main Nov 24, 2025
2 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.

5 participants