BoundaryML/baml · error · LspError

-32002

-32002

Error message

Server not initialized: {0}

What it means

LspError::ServerNotInitialized is returned when a request arrives before the initialize handshake has completed. Per the LSP specification, only 'initialize' and 'exit' (plus trivial notifications) are legal in this state; anything else is rejected with code -32002 and a message prefixed 'Server not initialized:'. The server throws this to enforce the mandated startup ordering.

Source

Thrown at baml_language/crates/baml_lsp/src/error.rs:61

    RequestCanceled(String),
    /// The request's snapshot became stale under an applied source change
    /// (LSP `ContentModified`, `-32801`).
    #[error("Content modified: {0}")]
    ContentModified(String),
    /// A valid request that cannot be served right now (LSP `RequestFailed`,
    /// `-32803`).
    #[error("{0}")]
    RequestFailed(String),
    /// Violated invariants, panics, serialization (LSP `InternalError`,
    /// `-32603`).
    #[error("Internal error: {0}")]
    Internal(String),
    /// Malformed params, position, or range (LSP `InvalidParams`, `-32602`).
    #[error("Invalid params: {0}")]
    InvalidParams(String),
    /// A request before `initialize` completed (LSP `ServerNotInitialized`,
    /// `-32002`).
    #[error("Server not initialized: {0}")]
    ServerNotInitialized(String),
}

impl LspError {
    /// The one LSP error-code mapping.
    #[must_use]
    pub fn to_response_error(&self) -> lsp_server::ResponseError {
        use lsp_server::ErrorCode;
        let code = match self {
            LspError::NotificationExtractError(_)
            | LspError::RequestExtractError(_)
            | LspError::InvalidCommandArguments { .. }
            | LspError::InvalidParams(_) => ErrorCode::InvalidParams,
            LspError::NotificationNotSupported(_) | LspError::RequestNotSupported(_) => {
                ErrorCode::MethodNotFound
            }
            LspError::ServerNotInitialized(_) => ErrorCode::ServerNotInitialized,
            LspError::RequestCanceled(_) => ErrorCode::RequestCanceled,

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Await the successful response to the initialize request before sending any other requests.
  2. Send the initialized notification after initialize completes, then begin normal requests.
  3. Queue requests client-side until initialization finishes instead of firing them at startup.
  4. If using a framework, ensure its LSP client lifecycle (initialize -> initialized -> ready) is respected.

Example fix

// before
spawnServer();
request('textDocument/hover', params); // too early
// after
await request('initialize', { capabilities: clientCapabilities });
notify('initialized', {});
request('textDocument/hover', params);
Defensive patterns

Strategy: try-catch

Validate before calling

let initialized = false;
function assertReady() {
  if (!initialized) throw new Error('LSP client not yet initialized');
}

Try / catch

try {
  return await request(method, params);
} catch (e) {
  if (e.code === -32002 || /Server not initialized:/.test(e.message ?? '')) {
    await initializeAndReady();
    return request(method, params); // retry once initialized
  }
  throw e;
}

Prevention

When it happens

Trigger: Client sends textDocument/*, workspace/*, or other requests before receiving the initialize response; the initialize response is lost or the client ignores it; a reconnecting client resumes requests without re-running initialize.

Common situations: Custom LSP clients that fire requests immediately after spawning the server process; race between editor startup and server readiness; a crashed initialize handshake leaving the server waiting.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/a5c30bf7a7607bd2. Report an issue: GitHub.