micro/go-micro · error

no response from API

Error message

no response from API

What it means

The API returned valid JSON that parsed correctly, but the choices array was empty, so there is no completion to return. The library treats an empty choices list as an unrecoverable empty response rather than returning a nil reply.

Source

Thrown at ai/openai/openai.go:353

			Message struct {
				Content   string `json:"content"`
				ToolCalls []struct {
					ID       string `json:"id"`
					Function struct {
						Name      string `json:"name"`
						Arguments string `json:"arguments"`
					} `json:"function"`
				} `json:"tool_calls"`
			} `json:"message"`
		} `json:"choices"`
	}

	if err := json.Unmarshal(respBody, &chatResp); err != nil {
		return nil, nil, fmt.Errorf("failed to parse response: %w", err)
	}

	if len(chatResp.Choices) == 0 {
		return nil, nil, fmt.Errorf("no response from API")
	}

	choice := chatResp.Choices[0]
	response := &ai.Response{
		Reply: choice.Message.Content,
		Usage: ai.Usage{InputTokens: chatResp.Usage.PromptTokens, OutputTokens: chatResp.Usage.CompletionTokens, TotalTokens: chatResp.Usage.TotalTokens},
	}

	// Extract tool calls
	for _, tc := range choice.Message.ToolCalls {
		var input map[string]any
		if err := json.Unmarshal([]byte(tc.Function.Arguments), &input); err != nil {
			input = map[string]any{}
		}
		response.ToolCalls = append(response.ToolCalls, ai.ToolCall{
			ID:    tc.ID,
			Name:  tc.Function.Name,
			Input: input,

View on GitHub (pinned to 24529f1404)

Solutions

  1. Log the full respBody to see whether the server explained the empty response
  2. Verify the model name is valid for the endpoint (e.g. gpt-4o, gpt-3.5-turbo)
  3. Re-check request parameters (prompt length, max tokens) and retry with a simple prompt
  4. If using a proxy/gateway to OpenAI, hit the upstream API directly to isolate the empty-response source

Example fix

// before
req := map[string]any{"model": "gpt-4o-mini-x", "messages": msgs} // typo'd model
// after
req := map[string]any{"model": "gpt-4o-mini", "messages": msgs}
Defensive patterns

Strategy: fallback

Validate before calling

// pre-flight: confirm the model exists
req, _ := http.NewRequest("GET", baseURL+"/v1/models", nil)
req.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := http.DefaultClient.Do(req)
// check the chosen model id appears in resp body

Try / catch

resp, err := provider.Generate(ctx, req)
if err != nil && strings.Contains(err.Error(), "no response from API") {
    log.Printf("empty choices; model=%s — retrying with fallback model", model)
    return fallbackProvider.Generate(ctx, req)
}

Prevention

When it happens

Trigger: callAPI unmarshals chatResp and len(chatResp.Choices) == 0 — typically because the upstream service returned an empty/success-shaped body with no candidates, e.g. content filtered, max_tokens=0-ish configuration, or an unknown/gateway default response.

Common situations: Prompt content flagged by moderation on some compatible providers, model name misspelled so a gateway returns an empty stub, gateway/load-balancer returning 200 with empty body on internal errors, requesting with n=0 or degenerate parameters.

Understand the failure class

Background: "empty response", "returned no data", "empty embeddings": what HTTP 200-with-empty-body errors mean across libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/886f1b8d0dd45a83. Report an issue: GitHub.