tim072844 on Discord: a custom provider on OpenAI compatible (Atria,
Shanghai AI Lab's Intern discovery API, model Atria-Dawn) answered
DeepSeek Harness with 238 validation errors saying `input` must be a
string. That "input" is pydantic's wording for the value it rejected,
not a Responses field: DeepSeek Harness (pi-ai's openai-completions)
sends every user message's content as a list of text parts, the gateway
relays a Chat client's body as it came, and that server's Chat schema
takes content as a string alone.
passthrough's Chat case now runs textPartsAsString: a content list made
only of {"type":"text","text":...} parts goes as their text joined by a
newline (as vLLM renders such a list). A list with an image, a file, a
thinking part, cache_control or any other key stays as it is. Every
other byte of the body stays as sent, and a body with no such list
comes back byte for byte. This covers every Chat-protocol upstream, not
Atria alone; buildChat's translated requests already sent strings.
TestChatRelayLeavesOutEmptyAssistant's "blank parts" case now expects
the joined user text as a string, since the parts are flattened before
silentAssistantsOut joins the two user turns.
Run (on mote, worktree at this commit's tree):
- go test -run 'TestChatRelayTextPartsAsString|TestTextPartsAsStringOnlyPlainText|TestChatRelayLeavesOutEmptyAssistant' ./internal/gateway: ok
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway: ok (553.6s); again after rebasing on 56d26f0bb, ok (603.4s)
- gofmt -l: clean; go vet darwin, linux (nogui), windows: ok
Broken by hand: with the textPartsAsString call commented out,
TestChatRelayTextPartsAsString is red (422 from the fake, the same
"Input should be a valid string" per list); green with it back.
Not tried: no Atria key, so no real request. The vendor answers 401
before validating a request without one, so its exact schema is
inferred from the reported error wording and pi-ai's source, not seen.
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// textPartsAsString turns a Chat request's message content that is a list
|
|
// of plain text parts ([{"type":"text","text":"…"}]) into the string the
|
|
// parts say, for a request relayed as the client sent it. OpenAI's Chat
|
|
// API takes either for the same text, but a vendor's own OpenAI-compatible
|
|
// server may take a string alone: Shanghai AI Lab's Intern discovery API
|
|
// (a custom provider, Atria-Dawn, tim072844 on Discord) answered DeepSeek
|
|
// Harness, whose pi-ai sends every user message as such a list, with a
|
|
// validation error per message ("Input should be a valid string", 238 of
|
|
// them), and every turn failed. magpie's own Chat requests, built from
|
|
// another API, already send text as a string (buildChat).
|
|
//
|
|
// Only a list of text parts with nothing else in them is joined, with a
|
|
// newline between parts, as vLLM's chat template renders such a list: a
|
|
// part with an image, a file, a thinking part or a cache_control mark
|
|
// (OpenRouter's Claude cache points) keeps the whole list as it is. Every
|
|
// other byte of the body stays as sent, and a body with no such list
|
|
// comes back as it was.
|
|
func textPartsAsString(body []byte) []byte {
|
|
if !bytes.Contains(body, contentParts) && !bytes.Contains(body, contentPartsSpace) {
|
|
return body
|
|
}
|
|
msgs := gjson.GetBytes(body, "messages")
|
|
if !msgs.IsArray() {
|
|
return body
|
|
}
|
|
type edit struct {
|
|
at, n int
|
|
s []byte
|
|
}
|
|
var edits []edit
|
|
msgs.ForEach(func(_, m gjson.Result) bool {
|
|
if !m.IsObject() {
|
|
return true
|
|
}
|
|
c := m.Get("content")
|
|
if !c.IsArray() || c.Index <= 0 || c.Index+len(c.Raw) > len(body) || string(body[c.Index:c.Index+len(c.Raw)]) != c.Raw {
|
|
return true
|
|
}
|
|
text, ok := plainTextParts(c)
|
|
if !ok {
|
|
return true
|
|
}
|
|
s, err := marshalPlain(text)
|
|
if err != nil {
|
|
return true
|
|
}
|
|
edits = append(edits, edit{c.Index, len(c.Raw), s})
|
|
return true
|
|
})
|
|
if len(edits) == 0 {
|
|
return body
|
|
}
|
|
out := make([]byte, 0, len(body))
|
|
last := 0
|
|
for _, e := range edits {
|
|
out = append(out, body[last:e.at]...)
|
|
out = append(out, e.s...)
|
|
last = e.at + e.n
|
|
}
|
|
return append(out, body[last:]...)
|
|
}
|
|
|
|
// plainTextParts is the text of a content list whose every part is
|
|
// {"type":"text","text":"…"} and nothing more, its parts joined by a
|
|
// newline; ok is false for an empty list or any other part.
|
|
func plainTextParts(c gjson.Result) (string, bool) {
|
|
var texts []string
|
|
ok := true
|
|
c.ForEach(func(_, p gjson.Result) bool {
|
|
if !p.IsObject() {
|
|
ok = false
|
|
return false
|
|
}
|
|
isText, hasText := false, false
|
|
p.ForEach(func(k, v gjson.Result) bool {
|
|
switch k.Str {
|
|
case "type":
|
|
isText = v.Type == gjson.String && v.Str == "text"
|
|
case "text":
|
|
hasText = v.Type == gjson.String
|
|
if hasText {
|
|
texts = append(texts, v.Str)
|
|
}
|
|
default:
|
|
isText = false
|
|
return false
|
|
}
|
|
return isText || k.Str == "text"
|
|
})
|
|
ok = isText && hasText
|
|
return ok
|
|
})
|
|
if !ok || len(texts) == 0 {
|
|
return "", false
|
|
}
|
|
return strings.Join(texts, "\n"), true
|
|
}
|