astral-sh/ruff · error · Error

InvalidParams

InvalidParams

Error message

JSON parsing failure:
{json_err}

What it means

Deserializing a request's `params` into the strongly-typed LSP struct failed: request.extract() returned ExtractError::JsonError, so the server answers with InvalidParams and the serde error text (api.rs:493). The method name matched a handler; the payload did not match the schema.

Source

Thrown at crates/ty_server/src/server/api.rs:493

/// Tries to cast a serialized request from the server into
/// a parameter type for a specific request handler.
/// It is *highly* recommended to not override this function in your
/// implementation.
fn cast_request<Req>(
    request: server::Request,
) -> Result<(
    RequestId,
    <<Req as RequestHandler>::RequestType as Request>::Params,
)>
where
    Req: RequestHandler,
    <<Req as RequestHandler>::RequestType as Request>::Params: UnwindSafe,
{
    request
        .extract(Req::METHOD.as_str())
        .map_err(|err| match err {
            json_err @ server::ExtractError::JsonError { .. } => {
                anyhow::anyhow!("JSON parsing failure:\n{json_err}")
            }
            server::ExtractError::MethodMismatch(_) => {
                unreachable!(
                    "A method mismatch should not be possible here \
                    unless you've used a different handler (`Req`) \
                    than the one whose method name was matched against earlier."
                )
            }
        })
        .with_failure_code(server::ErrorCode::InvalidParams)
}

/// Sends back a response to the client, but only if the request wasn't cancelled.
fn respond<Req>(
    id: &RequestId,
    result: Result<<<Req as RequestHandler>::RequestType as Request>::Result>,
    client: &Client,
    log_guidance: &str,

View on GitHub (pinned to 672bb4edf0)

Solutions

  1. Compare the sent JSON against the LSP spec types for that exact method
  2. Upgrade the client's lsp-types/protocol library to match the server's spec version
  3. Log the raw outgoing JSON payload and deserialize it locally with the same types to see the field error
  4. Ensure required fields are present and numeric fields are integers

Example fix

// before
sendRequest('textDocument/hover', {
  textDocument: { uri },
  position: { line: '0', character: 0 },  // string line -> JsonError
});

// after
sendRequest('textDocument/hover', {
  textDocument: { uri },
  position: { line: 0, character: 0 },
});
Defensive patterns

Strategy: validation

Validate before calling

// TS: assert the shape of params against the spec before sending
function assertHoverParams(p: unknown): asserts p is { textDocument: { uri: string }; position: { line: number; character: number } } {
  const o = p as any;
  if (typeof o?.textDocument?.uri !== 'string') throw new Error('bad textDocument.uri');
  if (!Number.isInteger(o?.position?.line) || !Number.isInteger(o?.position?.character))
    throw new Error('position.line/character must be integers');
}

Type guard

const isValidHoverParams = (p: unknown): p is HoverParams =>
  typeof (p as any)?.textDocument?.uri === 'string' &&
  Number.isInteger((p as any)?.position?.line) &&
  Number.isInteger((p as any)?.position?.character);

Try / catch

// TS: catch the InvalidParams response and log the serde detail
try { await client.sendRequest(method, params); }
catch (e: any) {
  if (e?.code === -32602) { log.error(`bad params for ${method}: ${e.message}`, params); return; }
  throw e;
}

Prevention

When it happens

Trigger: Sending a request whose params have wrong field types or missing required fields — e.g. a Position with string line numbers, or textDocument/hover without a valid TextDocumentIdentifier.

Common situations: Hand-rolled LSP clients with sloppy serialization, mismatched protocol-type versions between client library and server, custom fields accidentally replacing spec fields, or JSON numbers overflowing into floats.

Related errors


AI-assisted analysis of astral-sh/ruff@672bb4edf0 (2026-08-16). Data as JSON: /api/errors/31c34ac774762b9d. Report an issue: GitHub.