When integrating with external AI/HTTP APIs, treat both transport errors and payload validity as first-class failures. Validate inputs, verify provider/HTTP status, and enforce response invariants—never silently accept partial or malformed data.
Apply these rules:
Example pattern (fail fast + invariant checks):
resp, err := client.Do(req)
if err != nil { return nil, fmt.Errorf("send request: %w", err) }
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil { return nil, fmt.Errorf("read response: %w", err) }
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API error: %s body=%s", resp.Status, string(body))
}
// provider-level status (if present)
// var base BaseResp; json.Unmarshal(body,&base); if base.StatusCode!=0 { return nil, ... }
// invariants
embeddings := make([][]float64, len(texts))
for _, item := range parsed.Data {
if item.Index < 0 || item.Index >= len(texts) {
return nil, fmt.Errorf("unexpected index %d for %d inputs", item.Index, len(texts))
}
if len(item.Embedding) == 0 {
return nil, fmt.Errorf("empty embedding for index %d", item.Index)
}
// convert with type guards to float64
}
Goal: errors should be deterministic and informative, and incorrect provider payloads should never silently degrade results (e.g., via skipped indices or indistinguishable default zeros).
Enter the URL of a public GitHub repository