github/copilot-sdk · error

session.create returned sessionId

Error message

session.create returned sessionId %s but the caller requested %s

What it means

When CreateSession is called with a caller-specified local session ID and the server returns a different sessionId, the client unregisters the just-created session and returns this mismatch error. It enforces that the server honored the requested identifier; silently accepting a different ID would break the caller's bookkeeping.

Solutions

  1. Remove the explicit local session ID option and let the server assign one
  2. Verify the server supports caller-supplied session IDs and echoes them back
  3. Use the returned response.SessionID instead of your local ID for subsequent calls
  4. Check for stale state where the requested ID already exists server-side

Example fix

// before
opts.LocalSessionID = "my-fixed-id"
s, _ := c.CreateSession(ctx, opts)
// after
s, _ := c.CreateSession(ctx, opts) // use s.SessionID downstream
Defensive patterns

Strategy: validation

Validate before calling

if localID != "" {
    // only pass a local session ID if the server is known to honor caller-supplied IDs
    if !serverSupportsCallerSessionIDs { localID = "" }
}

Try / catch

s, err := client.CreateSession(ctx, opts)
if err != nil && strings.Contains(err.Error(), "but the caller requested") {
    // drop the explicit ID and retry
    opts.LocalSessionID = ""
    s, err = client.CreateSession(ctx, opts)
}

Prevention

When it happens

Trigger: CreateSession with a non-empty localSessionID option while the server's session.create response returns a different non-empty response.SessionID.

Common situations: Passing a pre-chosen session ID for resumability while the server generates its own IDs; reusing IDs across server restarts; ID collisions resolved server-side by minting a new ID.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/d2d06cd0b0a12654. Report an issue: GitHub.

Appendix: source

Thrown at go/client.go:1161

		}
		return nil, fmt.Errorf("failed to create session: %w", err)
	}

	var response createSessionResponse
	if err := json.Unmarshal(result, &response); err != nil {
		if registeredSessionID != "" {
			unregisterSession(registeredSessionID, session)
		}
		return nil, fmt.Errorf("failed to unmarshal response: %w", err)
	}

	if session == nil {
		return nil, fmt.Errorf("session.create response did not include a sessionId")
	}

	if localSessionID != "" && response.SessionID != "" && response.SessionID != localSessionID {
		unregisterSession(registeredSessionID, session)
		return nil, fmt.Errorf("session.create returned sessionId %s but the caller requested %s", response.SessionID, localSessionID)
	}
	if config.OnMCPAuthRequest != nil {
		if _, err := c.client.Request(ctx, "session.eventLog.registerInterest", map[string]any{
			"sessionId": session.SessionID,
			"eventType": "mcp.oauth_required",
		}); err != nil {
			unregisterSession(registeredSessionID, session)
			return nil, err
		}
	}

	session.workspacePath = response.WorkspacePath
	session.setCapabilities(response.Capabilities)

	if err := c.updateSessionOptionsForMode(ctx, session, optBackInFields{
		SkipCustomInstructions: config.SkipCustomInstructions,
		CustomAgentsLocalOnly:  config.CustomAgentsLocalOnly,
		CoauthorEnabled:        config.CoauthorEnabled,

View on GitHub (pinned to cd8cf15dc3)