Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amazon.Runtime.Documents.InvalidDocumentTypeConversionException #3694

Open
1 task
phspies opened this issue Mar 6, 2025 · 8 comments
Open
1 task

Amazon.Runtime.Documents.InvalidDocumentTypeConversionException #3694

phspies opened this issue Mar 6, 2025 · 8 comments
Labels
bug This issue is a bug. module/sdk-generated needs-review p1 This is a high priority issue

Comments

@phspies
Copy link

phspies commented Mar 6, 2025

Describe the bug

When using the the RetrievalFilter class for a Bedrock KnowledgeBase request it seems the key, value requires that you use the Amazon.Runtime.Documents.Document class within the filter structure. When attempting to use this filtering class the KnowledgeBase service seemed to ignore my filters but after further investigation, it seems the document class cannot be serialized. While trying to troubleshoot the problem I got this exception:

Amazon.Runtime.Documents.InvalidDocumentTypeConversionException: Cannot Convert DocumentType to List because it is String

I tried using this Document class multiple ways but it fails all the same:

retrievalFilter.Equals = new FilterAttribute
                {
                    Key = "TeamId",
                    Value = new Document(teamsId.ToString())
                };

This also fails

retrievalFilter.Equals = new FilterAttribute
                {
                    Key = "TeamId",
                    Value = teamsId.ToString()
                };

When I serialize the object using Serliog's internal serializer, it works but my filter values are empty:

{
    "Input": {
        "Text": "what is the diagrams about",
        "$type": "RetrieveAndGenerateInput"
    },
    "RetrieveAndGenerateConfiguration": {
        "ExternalSourcesConfiguration": null,
        "KnowledgeBaseConfiguration": {
            "GenerationConfiguration": null,
            "KnowledgeBaseId": "28W0R6NFGO",
            "ModelArn": "anthropic.claude-3-sonnet-20240229-v1:0",
            "OrchestrationConfiguration": null,
            "RetrievalConfiguration": {
                "VectorSearchConfiguration": {
                    "Filter": {
                        "AndAll": [
                            {
                                "AndAll": null,
                                "Equals": {
                                    "Key": "TeamId",
                                    "Value": [
                                        null
                                    ],
                                    "$type": "FilterAttribute"
                                },
                                "GreaterThan": null,
                                "GreaterThanOrEquals": null,
                                "In": null,
                                "LessThan": null,
                                "LessThanOrEquals": null,
                                "ListContains": null,
                                "NotEquals": null,
                                "NotIn": null,
                                "OrAll": null,
                                "StartsWith": null,
                                "StringContains": null,
                                "$type": "RetrievalFilter"
                            },
                            {
                                "AndAll": null,
                                "Equals": {
                                    "Key": "MeetingId",
                                    "Value": [
                                        null
                                    ],
                                    "$type": "FilterAttribute"
                                },
                                "GreaterThan": null,
                                "GreaterThanOrEquals": null,
                                "In": null,
                                "LessThan": null,
                                "LessThanOrEquals": null,
                                "ListContains": null,
                                "NotEquals": null,
                                "NotIn": null,
                                "OrAll": null,
                                "StartsWith": null,
                                "StringContains": null,
                                "$type": "RetrievalFilter"
                            }
                        ],
                        "Equals": null,
                        "GreaterThan": null,
                        "GreaterThanOrEquals": null,
                        "In": null,
                        "LessThan": null,
                        "LessThanOrEquals": null,
                        "ListContains": null,
                        "NotEquals": null,
                        "NotIn": null,
                        "OrAll": null,
                        "StartsWith": null,
                        "StringContains": null,
                        "$type": "RetrievalFilter"
                    },
                    "ImplicitFilterConfiguration": null,
                    "NumberOfResults": null,
                    "OverrideSearchType": null,
                    "RerankingConfiguration": null,
                    "$type": "KnowledgeBaseVectorSearchConfiguration"
                },
                "$type": "KnowledgeBaseRetrievalConfiguration"
            },
            "$type": "KnowledgeBaseRetrieveAndGenerateConfiguration"
        },
        "Type": {
            "Value": "KNOWLEDGE_BASE",
            "$type": "RetrieveAndGenerateType"
        },
        "$type": "RetrieveAndGenerateConfiguration"
    },
    "SessionConfiguration": null,
    "SessionId": null,
    "$type": "RetrieveAndGenerateRequest"
}

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

Document class needs to serializable and should pass the Value defined into the payload.

Current Behavior

Document object is not serializable.

Reproduction Steps

In the details above.

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

4.0.0-Preview5

Targeted .NET Platform

.Net 8.0

Operating System and version

Lambda

@phspies phspies added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 6, 2025
@ashishdhingra ashishdhingra added module/sdk-generated needs-reproduction This issue needs reproduction. p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Mar 6, 2025
@ashishdhingra ashishdhingra self-assigned this Mar 6, 2025
@phspies
Copy link
Author

phspies commented Mar 6, 2025

Downgraded to v3.7.421.5 and it still persists.

@ashishdhingra
Copy link
Contributor

@phspies Good afternoon. Could you please share self-contained code to reproduce the issue? I notice that you are using Document class from Amazon.Runtime.Documents namespace. Are you using the correct type?

@phspies
Copy link
Author

phspies commented Mar 6, 2025

Image

That is what the FilterAttribute class is expecting yes. Is there another namespace I need to use here?

@ashishdhingra
Copy link
Contributor

That is what the FilterAttribute class is expecting yes. Is there another namespace I need to use here?

@phspies Good morning. I tried testing the below code in a Console application and stepped into RetrieveAndGenerateRequestMarshaller while debugging via Visual Studio:

using Amazon.BedrockAgentRuntime;
using Amazon.BedrockAgentRuntime.Model;

var client = new AmazonBedrockAgentRuntimeClient();
var retrieveAndGenerateRequest = new RetrieveAndGenerateRequest()
{
    Input = new RetrieveAndGenerateInput()
    {
        Text = "what is the diagrams about"
    },
    RetrieveAndGenerateConfiguration = new RetrieveAndGenerateConfiguration()
    {
        Type = RetrieveAndGenerateType.KNOWLEDGE_BASE,
         KnowledgeBaseConfiguration = new KnowledgeBaseRetrieveAndGenerateConfiguration()
         {
            KnowledgeBaseId = "",
            ModelArn = "",
            RetrievalConfiguration = new KnowledgeBaseRetrievalConfiguration()
            {
                VectorSearchConfiguration = new KnowledgeBaseVectorSearchConfiguration()
                {
                    Filter = new RetrievalFilter()
                    {
                        AndAll = new List<RetrievalFilter>()
                        {
                            new RetrievalFilter()
                            {
                                Equals = new FilterAttribute()
                                {
                                    Key = "TeamId",
                                    Value = new Amazon.Runtime.Documents.Document("TestTeamId")
                                }
                            },
                            new RetrievalFilter()
                            {
                                Equals = new FilterAttribute()
                                {
                                    Key = "MeetingId",
                                    Value = new Amazon.Runtime.Documents.Document("TestMeetingId")
                                }
                            }
                        }
                    }
                }
            }
         }
    }
};

var response = await client.RetrieveAndGenerateAsync(retrieveAndGenerateRequest);
Console.Read();

Inspecting the value of request JSON generated here, it had the below contents:

{
  "input": {
    "text": "what is the diagrams about"
  },
  "retrieveAndGenerateConfiguration": {
    "knowledgeBaseConfiguration": {
      "knowledgeBaseId": "",
      "modelArn": "",
      "retrievalConfiguration": {
        "vectorSearchConfiguration": {
          "filter": {
            "andAll": [
              {
                "equals": {
                  "key": "TeamId",
                  "value": "TestTeamId"
                }
              },
              {
                "equals": {
                  "key": "MeetingId",
                  "value": "TestMeetingId"
                }
              }
            ]
          }
        }
      }
    },
    "type": "KNOWLEDGE_BASE"
  }
}

Could you please share the self-contained reproducible code as a Lambda project that we could use to troubleshoot the issue? Most likely it is a serializer issue.

Thanks,
Ashish

@ashishdhingra ashishdhingra added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 7, 2025
@martinsafsten-visma
Copy link

martinsafsten-visma commented Mar 14, 2025

I got this error in another part of the Bedrock agent response, specifically in TracePart.Trace.OrchestrationTrace.Observation.KnowledgeBaseLookupOutput.RetrievedReferences.Metadata

Simple repro using System.Json.Text.JsonSerializer:

var retrievalFilter = new Amazon.BedrockAgentRuntime.Model.RetrievalFilter
{
	Equals = new Amazon.BedrockAgentRuntime.Model.FilterAttribute
	{
		Key = "test",
		Value = new Amazon.Runtime.Documents.Document("test_document")
	}
};
var causesException = System.Text.Json.JsonSerializer.Serialize(retrievalFilter);
var retrievedReference = new Amazon.BedrockAgentRuntime.Model.RetrievedReference
{
	Metadata = new Dictionary<string, Amazon.Runtime.Documents.Document>
	{
		{"test", new Amazon.Runtime.Documents.Document("test_document")}
	}
};
var causesException = System.Text.Json.JsonSerializer.Serialize(retrievedReference);

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 15, 2025
@jooooel
Copy link

jooooel commented Mar 15, 2025

Hitting this as well.

<PackageVersion Include="AWSSDK.Bedrock" Version="3.7.413.9" />
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="3.7.412.5" />
var schema = new
{
    whatever = new
    {
        someInt = 123,
        someString = "hello world"
    }
};
var document  = Amazon.Runtime.Documents.Document.FromObject(schema);
var json = System.Text.Json.JsonSerializer.Serialize(document);

@or-unleash
Copy link

Same here,
When serializing a response that I got from ConverseAPI.

var response = await client.ConverseAsync(request);
var causesException = jsonSerializer.Serialize(response);

Any update?
@ashishdhingra

@ashishdhingra
Copy link
Contributor

Reproducible using code in #3694 (comment) and #3694 (comment).

When code in Document class tries to get enumerator, as part of AsList() it asserts that Type of document is of type List.

As users pointed out, we would need to refactor code in Document class to make it serializable. Needs review with the team.

@ashishdhingra ashishdhingra added needs-review and removed needs-reproduction This issue needs reproduction. labels Apr 7, 2025
@ashishdhingra ashishdhingra removed their assignment Apr 7, 2025
@eddiemcs3 eddiemcs3 added p1 This is a high priority issue and removed p2 This is a standard priority issue labels Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/sdk-generated needs-review p1 This is a high priority issue
Projects
None yet
Development

No branches or pull requests

6 participants