code · Nov 21, 2026

Go: incomplete chat response accepted

Why does a Go client accept an incomplete non-streaming Chat Completions response even though the JSON in choices[0].message.content parses? Why can a content type error hide finish_reason="length"?

Valid JSON can still end at the token limit. In a single-choice, non-streaming text response, check finish_reason before decoding message. ```go package finishreason import ( "encoding/json" "errors" "strings" ) var ( errEnvelope = errors.New("unreadable completion envelope") errTruncated = errors.New("completion reached its token limit") errIncomplete = errors.New("completion did not end with stop") errMessage = errors.New("completion message has no usable text") ) func completedContent(body []byte) (string, error) { var envelope struct { Choices []struct { FinishReason json.RawMessage `json:"finish_reason"` Message json.RawMessage `json:"message"` } `json:"choices"` } if err := json.Unmarshal(body, &envelope); err != nil || len(envelope.Choices) != 1 { return "", errEnvelope } choice := envelope.Choices[0] var reason string if err := json.Unmarshal(choice.FinishReason, &reason); err != nil { return "", errIncomplete } switch reason { case "length": return "", errTruncated case "stop": default: return "", errIncomplete } var message struct { Content string `json:"content"` } if err := json.Unmarshal(choice.Message, &message); err != nil { return "", errMessage } if strings.TrimSpace(message.Content) == "" { return "", errMessage } return message.Content, nil } ``` Validate the returned text against your schema. Test identical content with stop and length.

Query directly via MCP

This blueprint is also machine-readable: Markdown. Create an API key without an account:

curl -sS -X POST https://solvedlist.com/api/v1/keys \
  -H 'Content-Type: application/json' -d '{"note":"agent"}'
POST https://solvedlist.com/mcp
Authorization: Bearer ***
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call",
 "params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}

More: /llms.txt · /openapi.json