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

Document examples for UnmarshalSmithyDocument don't work #2751

Open
mgomes opened this issue Aug 16, 2024 · 3 comments
Open

Document examples for UnmarshalSmithyDocument don't work #2751

mgomes opened this issue Aug 16, 2024 · 3 comments
Assignees
Labels
bug This issue is a bug. documentation This is a problem with documentation. p2 This is a standard priority issue

Comments

@mgomes
Copy link

mgomes commented Aug 16, 2024

Describe the issue

Here is a minimally reproducible example of the issue I am running into:

package main

import (
	"fmt"
	"log"

	"github.com/aws/aws-sdk-go-v2/service/bedrockruntime/document"
)

func main() {
	doc := document.NewLazyDocument(map[string]interface{}{
		"param1": "value1",
	})

	var result map[string]interface{}
	err := doc.UnmarshalSmithyDocument(&result)
	if err != nil {
		log.Fatalf("Error unmarshalling Smithy document: %v", err)
	}

	fmt.Printf("Unmarshalled result: %+v\n", result)
}

Running this code produces the error: Error unmarshalling Smithy document: unsupported json type, *map[string]interface {}

Is NewLazyDocument storing the map as a pointer internally somehow and so it isn't getting recognized? It seems like it should work when looking at the decoder: https://github.com/aws/smithy-go/blob/main/document/json/decoder.go#L71.

Links

https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/omics/document

AWS Go SDK V2 Module Versions Used

1.23.4

@mgomes mgomes added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Aug 16, 2024
@RanVaknin RanVaknin self-assigned this Aug 19, 2024
@RanVaknin
Copy link
Contributor

Hi @mgomes ,

Can you give us a little more info about how you are trying to use this document unmarshalling?

Thanks,
Ran~

@RanVaknin RanVaknin added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Aug 22, 2024
@mgomes
Copy link
Author

mgomes commented Aug 22, 2024

Hi @RanVaknin 👋,

I am using it with tool use via the converse API. When a tool is selected by the model a ToolUseBlock is returned. The Input field contains the arguments that need to be passed to the respective tool. I am having a hard time getting those values out as is.

It seems to work in code, but in my unit test it fails.

My code looks something like this:

var args map[string]interface{}
err := bedrockToolUse.Value.Input.UnmarshalSmithyDocument(&args)
if err != nil {
    slog.Error("unmarshalling tool arguments.", slog.Any("error", err))
    return toolCalls
}

The test attempts to construct some sample arguments:

name: "Valid tool use transformation",
input: &types.ContentBlockMemberToolUse{
    Value: types.ToolUseBlock{
        ToolUseId: aws.String("tool-id"),
        Name:      aws.String("test-tool"),
        Input: document.NewLazyDocument(map[string]interface{}{
            "param1": "value1",
        }),
    },
}

But it fails with the same error as the reproduction code I pasted. Any recommendations for a workaround?

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Aug 23, 2024
@RanVaknin
Copy link
Contributor

Hi there,

Thanks for the clarification. Admittedly I was not too familiar with the document deserializer because there are very few services that use it. Looking at the deserializer source code that gets generated for each client, there are two UnmarshalSmithyDocument methods. You can mock this one:

func (m *documentUnmarshaler) UnmarshalSmithyDocument(v interface{}) error {
decoder := smithydocumentjson.NewDecoder()
return decoder.DecodeJSONInterface(m.value, v)
}

Because that is the one that the actual API client uses. So in your case:

package main

import (
	"fmt"
	smithydocumentjson "github.com/aws/smithy-go/document/json"
	"log"
)

type mockDoc struct {
	mockedValue interface{}
}

func (m *mockDoc) UnmarshalSmithyDocument(v interface{}) error {
	decoder := smithydocumentjson.NewDecoder()
	return decoder.DecodeJSONInterface(m.mockedValue, v)
}

func main() {

	doc := mockDoc{
		map[string]interface{}{
			"param1": "value1",
		},
	}

	var result map[string]interface{}
	err := doc.UnmarshalSmithyDocument(&result)
	if err != nil {
		log.Fatalf("Error unmarshalling Smithy document: %v", err)
	}

	fmt.Printf("Unmarshalled result: %+v\n", result)
}

// Unmarshalled result: map[param1:value1]

Thanks,
Ran~

@RanVaknin RanVaknin added the bug This issue is a bug. label Aug 23, 2024
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. documentation This is a problem with documentation. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

2 participants