micro/go-micro · error

failed to create request: %w

Error message

failed to create request: %w

What it means

http.NewRequestWithContext failed while building the POST to <BaseURL>/v1/chat/completions in the Together provider. With a valid bytes.Reader body, this points to URL parsing/validation failure of the assembled API URL.

Source

Thrown at ai/together/together.go:167

// round is a model call plus the tools it asks for, so this is the ceiling on
// one question's cost as well as its length; it is high enough that no honest
// piece of multi-step work reaches it.
const maxToolRounds = 12

func (p *Provider) Stream(ctx context.Context, req *ai.Request, opts ...ai.GenerateOption) (ai.Stream, error) {
	return openaiapi.Stream(ctx, p.opts, req, "/v1/chat/completions")
}

func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Response, map[string]any, error) {
	reqBody, err := json.Marshal(req)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to marshal request: %w", err)
	}

	apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/chat/completions"
	httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
	if err != nil {
		return nil, nil, fmt.Errorf("failed to create request: %w", err)
	}

	httpReq.Header.Set("Content-Type", "application/json")
	httpReq.Header.Set("Authorization", "Bearer "+p.opts.APIKey)

	httpResp, err := http.DefaultClient.Do(httpReq)
	if err != nil {
		return nil, nil, fmt.Errorf("API request failed: %w", err)
	}
	defer httpResp.Body.Close()

	respBody, _ := io.ReadAll(httpResp.Body)
	if httpResp.StatusCode != http.StatusOK {
		return nil, nil, ai.NewHTTPError(httpResp, respBody)
	}

	var chatResp struct {
		Choices []struct {

View on GitHub (pinned to 24529f1404)

Solutions

  1. Inspect the exact BaseURL string for hidden whitespace/quotes and TrimSpace it
  2. Ensure BaseURL is a clean absolute https URL (e.g. https://api.together.xyz)
  3. Add a startup config check that url.Parse succeeds on the base URL before use
  4. Log the fully assembled apiURL when this error occurs

Example fix

// before
baseURL := os.Getenv("TOGETHER_BASE_URL") // may contain "\n"
// after
baseURL := strings.TrimSpace(os.Getenv("TOGETHER_BASE_URL"))
Defensive patterns

Strategy: validation

Validate before calling

func validateTogetherURL(raw string) error {
    raw = strings.TrimSpace(raw)
    u, err := url.Parse(raw)
    if err != nil {
        return fmt.Errorf("invalid Together BaseURL: %w", err)
    }
    if u.Scheme != "https" || u.Host == "" {
        return fmt.Errorf("Together BaseURL must be absolute https URL, got %q", raw)
    }
    return nil
}

Try / catch

resp, err := provider.Generate(ctx, req)
if err != nil && strings.Contains(err.Error(), "failed to create request") {
    return fmt.Errorf("malformed Together BaseURL: %w", err)
}

Prevention

When it happens

Trigger: strings.TrimRight(p.opts.BaseURL, "/") + "/v1/chat/completions" produces a URL http.NewRequestWithContext cannot parse: BaseURL containing spaces, newlines, quotes, or invalid percent-encodings from config/env.

Common situations: TOGETHER base-URL env var with trailing newline/whitespace, copy-pasted URL including quotes, templated config injecting characters into the host.

Related errors


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