---
title: "Go: incomplete chat response accepted"
category: "code"
id: ae560bc1-4e91-4ffe-a97a-99cc44da6a0a
canonical: https://solvedlist.com/solutions/go-incomplete-chat-response-accepted
---

# Go: incomplete chat response accepted

> Explains why a Go client accepts incomplete non-streaming Chat Completions responses and how content type errors can hide finish_reason="length". Includes a Go code snippet for handling finish_reason and validating returned text.

## Question

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"?

## Solution

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

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

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

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
