@@ -19,6 +19,7 @@ import (
1919
2020 "github.com/anthropics/anthropic-sdk-go"
2121 "github.com/openai/openai-go/v2"
22+ "github.com/tidwall/gjson"
2223 "google.golang.org/genai"
2324
2425 "github.com/envoyproxy/ai-gateway/internal/apischema/awsbedrock"
@@ -142,15 +143,14 @@ type ChatCompletionContentPartUserUnionParam struct {
142143}
143144
144145func (c * ChatCompletionContentPartUserUnionParam ) UnmarshalJSON (data []byte ) error {
145- var chatContentPart map [string ]any
146- if err := json .Unmarshal (data , & chatContentPart ); err != nil {
147- return err
148- }
149- var contentType string
150- var ok bool
151- if contentType , ok = chatContentPart ["type" ].(string ); ! ok {
152- return fmt .Errorf ("chat content does not have type" )
146+ typeResult := gjson .GetBytes (data , "type" )
147+ if ! typeResult .Exists () {
148+ return errors .New ("chat content does not have type" )
153149 }
150+
151+ // Based on the 'type' field, unmarshal into the correct struct.
152+ contentType := typeResult .String ()
153+
154154 switch contentType {
155155 case string (ChatCompletionContentPartTextTypeText ):
156156 var textContent ChatCompletionContentPartTextParam
@@ -290,18 +290,14 @@ type ChatCompletionMessageParamUnion struct {
290290}
291291
292292func (c * ChatCompletionMessageParamUnion ) UnmarshalJSON (data []byte ) error {
293- var chatMessage map [string ]any
294- if err := json .Unmarshal (data , & chatMessage ); err != nil {
295- return err
296- }
297- if _ , ok := chatMessage ["role" ]; ! ok {
298- return fmt .Errorf ("chat message does not have role" )
299- }
300- var role string
301- var ok bool
302- if role , ok = chatMessage ["role" ].(string ); ! ok {
303- return fmt .Errorf ("chat message role is not string: %s" , role )
293+ roleResult := gjson .GetBytes (data , "role" )
294+ if ! roleResult .Exists () {
295+ return errors .New ("chat message does not have role" )
304296 }
297+
298+ // Based on the 'role' field, unmarshal into the correct struct.
299+ role := roleResult .String ()
300+
305301 switch role {
306302 case ChatMessageRoleUser :
307303 var userMessage ChatCompletionUserMessageParam
@@ -545,20 +541,13 @@ func (c ChatCompletionResponseFormatUnion) MarshalJSON() ([]byte, error) {
545541
546542// UnmarshalJSON implements the json.Unmarshaler interface for ChatCompletionResponseFormatUnion.
547543func (c * ChatCompletionResponseFormatUnion ) UnmarshalJSON (data []byte ) error {
548- // First, peek at the "type" field to determine the concrete type.
549- var responseFormatPart map [string ]any
550- if err := json .Unmarshal (data , & responseFormatPart ); err != nil {
551- return err
552- }
553-
554- var typeValue string
555- var ok bool
556- if typeValue , ok = responseFormatPart ["type" ].(string ); ! ok {
557- return fmt .Errorf ("response format does not have type" )
544+ typeResult := gjson .GetBytes (data , "type" )
545+ if ! typeResult .Exists () {
546+ return errors .New ("response format does not have type" )
558547 }
559548
560549 // Based on the 'type' field, unmarshal into the correct struct.
561- responseFormatType := ChatCompletionResponseFormatType (typeValue )
550+ responseFormatType := ChatCompletionResponseFormatType (typeResult . String () )
562551
563552 switch responseFormatType {
564553 case ChatCompletionResponseFormatTypeText :
0 commit comments