{"record":{"id":"8af84d4daf655c1a","repo":"github/copilot-sdk","slug":"session-create-returned-sessionid-response-sessio","errorCode":null,"errorMessage":"session.create returned sessionId {response.SessionId} but the caller requested {localSessionId}.","messagePattern":"session\\.create returned sessionId (.+?) but the caller requested (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Client.cs","lineNumber":1347,"sourceCode":"                        \"CopilotClient.CreateSessionAsync\");\n                }\n            };\n\n            var response = await InvokeRpcAsync<CreateSessionResponse>(\n                connection.Rpc, \"session.create\", [request], null, cancellationToken, onResponseInline);\n            LoggingHelpers.LogTiming(_logger, LogLevel.Debug, null,\n                \"CopilotClient.CreateSessionAsync session creation request completed successfully. Elapsed={Elapsed}, SessionId={SessionId}\",\n                rpcTimestamp,\n                response.SessionId);\n\n            if (session is null)\n            {\n                throw new InvalidOperationException(\"session.create response did not include a sessionId.\");\n            }\n\n            if (localSessionId != null && !string.Equals(localSessionId, response.SessionId, StringComparison.Ordinal))\n            {\n                throw new InvalidOperationException(\n                    $\"session.create returned sessionId {response.SessionId} but the caller requested {localSessionId}.\");\n            }\n\n            if (config.OnMcpAuthRequest is not null)\n            {\n                await session.Rpc.EventLog.RegisterInterestAsync(\"mcp.oauth_required\", cancellationToken);\n            }\n\n            session.WorkspacePath = response.WorkspacePath;\n            session.SetCapabilities(response.Capabilities);\n            session.SetOpenCanvases(response.OpenCanvases);\n\n            await UpdateSessionOptionsForModeAsync(session, config, cancellationToken).ConfigureAwait(false);\n            if (registrationId is not null)\n            {\n                session.SetGitHubTokenProviderRegistration(registrationId);\n                registrationTransferred = true;\n            }","sourceCodeStart":1329,"sourceCodeEnd":1365,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/dotnet/src/Client.cs#L1329-L1365","documentation":"This InvalidOperationException is thrown by CopilotClient when the CLI's session.create RPC response returns a sessionId that does not exactly match the sessionId the caller explicitly requested for the new session. The library treats a mismatched session ID as a protocol violation, since the caller supplied localSessionId and the server must honor it. It guards callers from silently using a session under the wrong ID.","triggerScenarios":"Calling session creation (e.g. CreateSessionAsync / StartSessionAsync) with a non-null localSessionId while the Copilot CLI server responds with a different SessionId; typically a server/CLI version that does not honor client-supplied session IDs, or a bug/misconfiguration in the connection.","commonSituations":"Running an older or newer Copilot CLI whose session.create implementation ignores or regenerates caller-supplied session IDs; two clients racing to create sessions with the same ID; a misrouted/proxied RPC connection replying with another session's response.","solutions":["Remove the explicit localSessionId argument and let the CLI generate the session ID, then use response.SessionId","Upgrade (or align) the Copilot CLI/server version so session.create honors caller-requested session IDs","Compare versions: if you pinned a session ID for resume semantics, use the session resume/reconnect API instead of session.create","Check for multiple connections/clients sharing the same RPC channel that could interleave responses"],"exampleFix":"// before\nvar session = await client.CreateSessionAsync(new SessionConfig { SessionId = myId });\n// after\nvar session = await client.CreateSessionAsync(new SessionConfig()); // let CLI assign; use session.Id afterwards","handlingStrategy":"validation","validationCode":"if (config.SessionId is not null && string.IsNullOrWhiteSpace(config.SessionId)) throw new ArgumentException(\"SessionId must be null or a non-empty exact id\");","typeGuard":"static bool IsValidRequestedSessionId(SessionConfig c) => c.SessionId is null || c.SessionId.Length > 0;","tryCatchPattern":"try { await client.CreateSessionAsync(config); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"session.create returned sessionId\"))\n{ logger.LogError(ex, \"CLI ignored requested session id; falling back to server id\"); /* recreate without localSessionId */ }","preventionTips":["Omit localSessionId unless you truly need a specific ID","Keep CLI and SDK versions aligned","Use session resume APIs instead of forcing IDs via session.create","Log response.SessionId on creation to detect mismatches early"],"tags":["session","rpc","protocol-mismatch","dotnet"],"backgroundTag":"unexpected-response-shape","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}