aws-sdk-go-v2 icon indicating copy to clipboard operation
aws-sdk-go-v2 copied to clipboard

Document examples for UnmarshalSmithyDocument don't work

Open mgomes opened this issue 1 year ago • 3 comments

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 avatar Aug 16 '24 15:08 mgomes

Hi @mgomes ,

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

Thanks, Ran~

RanVaknin avatar Aug 22 '24 17:08 RanVaknin

Hi @RanVaknin :wave:,

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?

mgomes avatar Aug 22 '24 18:08 mgomes

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:

https://github.com/aws/aws-sdk-go-v2/blob/d1d210dd11afc2ef0efa2e980d6b6c09cac0f5cb/service/bedrockruntime/internal/document/document.go#L60-L63

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 avatar Aug 23 '24 01:08 RanVaknin

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.

github-actions[bot] avatar Oct 29 '25 21:10 github-actions[bot]