Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions common/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ func TestHostJSONCodecConformance(t *testing.T) {
}
})

t.Run("gemini part keeps explicit empty text through DeepCopy", func(t *testing.T) {
input := `{"parts":[{"text":""},{"text":"","thoughtSignature":"c2ln"},{"functionCall":{"name":"f","args":{}},"thoughtSignature":"c2ln"}]}`
var content dto.GeminiChatContent
require.NoError(t, UnmarshalJsonStr(input, &content))
copied, err := DeepCopy(&content)
require.NoError(t, err)
encoded, err := Marshal(copied)
require.NoError(t, err)
assert.JSONEq(t, input, string(encoded))
})

t.Run("raw message passthrough", func(t *testing.T) {
input := `{"obj":{},"arr":[],"nested":{"k":[1,2]},"str":"x"}`
var value rawFields
Expand Down
27 changes: 27 additions & 0 deletions relaykit/dto/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ type GeminiPart struct {
FileData *GeminiFileData `json:"fileData,omitempty"`
ExecutableCode *GeminiPartExecutableCode `json:"executableCode,omitempty"`
CodeExecutionResult *GeminiPartCodeExecutionResult `json:"codeExecutionResult,omitempty"`
// textAbsent records that the part was decoded from JSON without a text
// member, so an unrecognized part is not re-marshaled as empty text.
textAbsent bool
}

// UnmarshalJSON accepts snake_case aliases for inline data and media processing.
Expand All @@ -357,6 +360,7 @@ func (p *GeminiPart) UnmarshalJSON(data []byte) error {
type Alias GeminiPart
var aux struct {
Alias
Text *string `json:"text"`
InlineDataSnake *GeminiInlineData `json:"inline_data,omitempty"`
MediaProcessingSnake json.RawMessage `json:"media_processing,omitempty"`
}
Expand All @@ -367,6 +371,11 @@ func (p *GeminiPart) UnmarshalJSON(data []byte) error {

// Assign fields from alias
*p = GeminiPart(aux.Alias)
if aux.Text != nil {
p.Text = *aux.Text
} else {
p.textAbsent = true
}

// Prioritize snake_case for InlineData if present
if aux.InlineDataSnake != nil {
Expand All @@ -382,6 +391,24 @@ func (p *GeminiPart) UnmarshalJSON(data []byte) error {
return nil
}

// MarshalJSON keeps an empty "text" when it is the part's only data field,
// unless the part was decoded without one. Gemini models Part.data as a proto
// oneof, so {"text": ""} is a valid text part (models emit it as a closing
// chunk, alone or with thoughtSignature), while omitempty would leave {} and
// upstream rejects the request with "required oneof field 'data' must have
// one initialized field".
func (p GeminiPart) MarshalJSON() ([]byte, error) {
type partFields GeminiPart
if p.textAbsent || p.Text != "" || p.InlineData != nil || p.FunctionCall != nil || p.FunctionResponse != nil ||
p.FileData != nil || p.ExecutableCode != nil || p.CodeExecutionResult != nil {
return kitutil.Marshal(partFields(p))
}
return kitutil.Marshal(struct {
Text string `json:"text"`
partFields
}{partFields: partFields(p)})
}

type GeminiChatContent struct {
Role string `json:"role,omitempty"`
Parts []GeminiPart `json:"parts"`
Expand Down
77 changes: 77 additions & 0 deletions relaykit/dto/gemini_part_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dto

import (
"testing"

kitutil "github.com/QuantumNous/new-api/relaykit/relayconvert/kitutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// Gemini's Part.data is a proto oneof: a re-marshaled part must never lose
// its only data field, or upstream rejects it as an uninitialized oneof.
func TestGeminiPartRoundTripKeepsOneofData(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{name: "bare empty text", in: `{"text":""}`, want: `{"text":""}`},
{name: "empty text with signature", in: `{"text":"","thoughtSignature":"c2ln"}`, want: `{"text":"","thoughtSignature":"c2ln"}`},
{name: "empty thought text", in: `{"text":"","thought":true}`, want: `{"text":"","thought":true}`},
{name: "text", in: `{"text":"hi"}`, want: `{"text":"hi"}`},
{name: "thought text with signature", in: `{"text":"plan","thought":true,"thoughtSignature":"c2ln"}`, want: `{"text":"plan","thought":true,"thoughtSignature":"c2ln"}`},
{name: "function call with signature", in: `{"functionCall":{"name":"f","args":{"a":1}},"thoughtSignature":"c2ln"}`, want: `{"functionCall":{"name":"f","args":{"a":1}},"thoughtSignature":"c2ln"}`},
{name: "function response", in: `{"functionResponse":{"name":"f","response":{"ok":true}}}`, want: `{"functionResponse":{"name":"f","response":{"ok":true}}}`},
{name: "snake case inline data", in: `{"inline_data":{"mime_type":"image/png","data":"AA=="}}`, want: `{"inlineData":{"mimeType":"image/png","data":"AA=="}}`},
{name: "null text", in: `{"text":null}`, want: `{}`},
{name: "unrecognized member is not turned into text", in: `{"file_data":{"file_uri":"gs://b/v.mp4"}}`, want: `{}`},
{name: "file data", in: `{"fileData":{"mimeType":"video/mp4","fileUri":"gs://b/v.mp4"}}`, want: `{"fileData":{"mimeType":"video/mp4","fileUri":"gs://b/v.mp4"}}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var part GeminiPart
require.NoError(t, kitutil.Unmarshal([]byte(tt.in), &part))

byValue, err := kitutil.Marshal(part)
require.NoError(t, err)
assert.JSONEq(t, tt.want, string(byValue))

byPointer, err := kitutil.Marshal(&part)
require.NoError(t, err)
assert.JSONEq(t, tt.want, string(byPointer))
})
}
}

func TestGeminiPartBuiltWithEmptyTextKeepsText(t *testing.T) {
data, err := kitutil.Marshal(GeminiPart{Text: ""})
require.NoError(t, err)
assert.JSONEq(t, `{"text":""}`, string(data))
}

func TestGeminiChatRequestKeepsTrailingEmptyTextPart(t *testing.T) {
in := `{"contents":[{"role":"model","parts":[` +
`{"text":"thinking","thought":true},` +
`{"functionCall":{"name":"search","args":{"q":"x"}},"thoughtSignature":"c2ln"},` +
`{"text":""}` +
`]}]}`
var req GeminiChatRequest
require.NoError(t, kitutil.Unmarshal([]byte(in), &req))

data, err := kitutil.Marshal(&req)
require.NoError(t, err)

var out struct {
Contents []struct {
Parts []map[string]any `json:"parts"`
} `json:"contents"`
}
require.NoError(t, kitutil.Unmarshal(data, &out))
require.Len(t, out.Contents, 1)
assert.Equal(t, []map[string]any{
{"text": "thinking", "thought": true},
{"functionCall": map[string]any{"name": "search", "args": map[string]any{"q": "x"}}, "thoughtSignature": "c2ln"},
{"text": ""},
}, out.Contents[0].Parts)
}