router-for-me/CLIProxyAPI · error

xai device code: response missing user_code

Error message

xai device code: response missing user_code

What it means

The device authorization response parsed correctly and contained a device_code, but user_code is empty. user_code is the short string the user must type at the verification URI; without it the interactive authorization step is impossible, so the client refuses to proceed.

Source

Thrown at internal/auth/xai/xai.go:169

	}()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("xai device code: read response: %w", err)
	}
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("xai device code request failed with status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
	}

	var deviceCode DeviceCodeResponse
	if err = json.Unmarshal(body, &deviceCode); err != nil {
		return nil, fmt.Errorf("xai device code: parse response: %w", err)
	}
	if strings.TrimSpace(deviceCode.DeviceCode) == "" {
		return nil, fmt.Errorf("xai device code: response missing device_code")
	}
	if strings.TrimSpace(deviceCode.UserCode) == "" {
		return nil, fmt.Errorf("xai device code: response missing user_code")
	}
	if strings.TrimSpace(deviceCode.VerificationURI) == "" && strings.TrimSpace(deviceCode.VerificationURIComplete) == "" {
		return nil, fmt.Errorf("xai device code: response missing verification URI")
	}
	deviceCode.TokenEndpoint = strings.TrimSpace(tokenEndpoint)
	return &deviceCode, nil
}

// WaitForAuthorization polls until the user authorizes the device code and returns tokens.
func (a *XAIAuth) WaitForAuthorization(ctx context.Context, deviceCode *DeviceCodeResponse) (*AuthBundle, error) {
	tokenData, err := a.PollForToken(ctx, deviceCode)
	if err != nil {
		return nil, err
	}
	tokenEndpoint := ""
	if deviceCode != nil {
		tokenEndpoint = strings.TrimSpace(deviceCode.TokenEndpoint)
	}

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Log the raw body alongside the error to see the full server payload
  2. Verify the request is going to the official xAI device authorization endpoint via a.Discover
  3. Check whether xAI renamed user_code (e.g. to a nested field) and update the DeviceCodeResponse struct if so
Defensive patterns

Strategy: validation

Try / catch

deviceCode, err := auth.RequestDeviceCode(ctx)
if err != nil {
    if strings.Contains(err.Error(), "response missing") {
        // server contract issue: log raw context and surface to the user
        log.Warnf("xAI device authorization response incomplete: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: POST to the xAI device authorization endpoint returns 200 JSON with device_code present but user_code absent or whitespace-only.

Common situations: Partial or evolving server response schema from xAI; a middleware/proxy stripping fields; a misconfigured client_id that yields a degraded response instead of a 4xx error.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/103790d89a6cb14b. Report an issue: GitHub.