github/copilot-sdk · error · InvalidOperationException

session.create response did not include a sessionId.

Error message

session.create response did not include a sessionId.

What it means

CreateSessionAsync throws this InvalidOperationException when the session.create RPC response completes successfully but the decoded session object (sessionId) is null. This indicates the server replied without the required sessionId field, i.e. a malformed or unexpected response payload.

Solutions

  1. Verify the Copilot CLI/runtime version matches the SDK's supported protocol version and update it.
  2. Log the raw session.create response to inspect what the server actually returned.
  3. Retry CreateSessionAsync in case of a transient malformed response.
  4. If using a mock or proxy server, fix it to include sessionId in the session.create result.

Example fix

// before (custom mock server)
result = new { }; // session.create response without sessionId
// after
result = new { sessionId = Guid.NewGuid().ToString() };
Defensive patterns

Strategy: retry

Validate before calling

// can only be checked after the RPC; validate response server-side in mocks:
// mock must return { sessionId: "..." } for session.create

Type guard

static bool HasSessionId(SessionResponse? response) => response?.Session is not null && !string.IsNullOrEmpty(response.Session.SessionId);

Try / catch

try { session = await client.CreateSessionAsync(config); }
catch (InvalidOperationException ex) when (ex.Message.Contains("did not include a sessionId"))
{
    logger.LogWarning("Malformed session.create response; retrying");
    session = await client.CreateSessionAsync(config); // bounded retry recommended
}

Prevention

When it happens

Trigger: Calling CreateSessionAsync when the runtime returns a session.create response missing sessionId - e.g. server/runtime version mismatch, a proxy or mock returning an empty payload, or a protocol deserialization gap.

Common situations: Pointing the SDK at an incompatible or very old runtime/CLI version; custom or mocked JSON-RPC servers in tests; network middleware stripping or truncating response fields.

Related errors


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

Appendix: source

Thrown at dotnet/src/Client.cs:1342

                        sessionId,
                        connection.Rpc,
                        config,
                        transformCallbacks,
                        hasHooks,
                        "CopilotClient.CreateSessionAsync");
                }
            };

            var response = await InvokeRpcAsync<CreateSessionResponse>(
                connection.Rpc, "session.create", [request], null, cancellationToken, onResponseInline);
            LoggingHelpers.LogTiming(_logger, LogLevel.Debug, null,
                "CopilotClient.CreateSessionAsync session creation request completed successfully. Elapsed={Elapsed}, SessionId={SessionId}",
                rpcTimestamp,
                response.SessionId);

            if (session is null)
            {
                throw new InvalidOperationException("session.create response did not include a sessionId.");
            }

            if (localSessionId != null && !string.Equals(localSessionId, response.SessionId, StringComparison.Ordinal))
            {
                throw new InvalidOperationException(
                    $"session.create returned sessionId {response.SessionId} but the caller requested {localSessionId}.");
            }

            if (config.OnMcpAuthRequest is not null)
            {
                await session.Rpc.EventLog.RegisterInterestAsync("mcp.oauth_required", cancellationToken);
            }

            session.WorkspacePath = response.WorkspacePath;
            session.SetCapabilities(response.Capabilities);
            session.SetOpenCanvases(response.OpenCanvases);

            await UpdateSessionOptionsForModeAsync(session, config, cancellationToken).ConfigureAwait(false);

View on GitHub (pinned to cd8cf15dc3)