langchain-ai/langchain · warning · ValueError

x should be >= 0 and < number of columns

Error message

x should be >= 0 and < number of columns

What it means

`AsciiCanvas.draw_on` bounds-checks the x coordinate: it must satisfy `0 <= x < cols`. An x at or beyond the canvas width, or negative, would index outside the grid, so `ValueError: x should be >= 0 and < number of columns` is raised (after the single-char check, before writing).

Source

Thrown at libs/core/langchain_core/runnables/graph_ascii.py:110

        Args:
            x: x coordinate. Should be `>= 0` and `<` number of columns in
                the canvas.
            y: y coordinate. Should be `>= 0` an `<` number of lines in the
                canvas.
            char: character to place in the specified point on the
                canvas.

        Raises:
            ValueError: if char is not a single character or if
                coordinates are out of bounds.
        """
        if len(char) != 1:
            msg = "char should be a single character"
            raise ValueError(msg)
        if x >= self.cols or x < 0:
            msg = "x should be >= 0 and < number of columns"
            raise ValueError(msg)
        if y >= self.lines or y < 0:
            msg = "y should be >= 0 and < number of lines"
            raise ValueError(msg)

        self.canvas[y][x] = char

    def line(self, x0: int, y0: int, x1: int, y1: int, char: str) -> None:
        """Create a line on ASCII canvas.

        Args:
            x0: x coordinate where the line should start.
            y0: y coordinate where the line should start.
            x1: x coordinate where the line should end.
            y1: y coordinate where the line should end.
            char: character to draw the line with.
        """
        if x0 > x1:
            x1, x0 = x0, x1

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Size the canvas to the layout: ensure `cols > max_x` and `lines > max_y` used by draw calls.
  2. Clamp coordinates: `x = min(max(x, 0), canvas.cols - 1)` when overflow is acceptable.
  3. With the public API, prefer `print(runnable.get_graph().draw_ascii())`, which sizes the canvas internally, over manual coordinate math.

Example fix

# before
canvas = AsciiCanvas(max_x, lines)  # max_x is a valid x -> out of bounds

canvas.draw_on(max_x, y, "#")

# after
canvas = AsciiCanvas(max_x + 1, lines)

canvas.draw_on(max_x, y, "#")
Defensive patterns

Strategy: validation

Validate before calling

assert 0 <= x < canvas.cols and 0 <= y < canvas.lines
canvas.draw_on(x, y, char)

Prevention

When it happens

Trigger: Calling `draw_on(cols, y, "#")` (off-by-one at the right edge), a negative x from a line-drawing algorithm whose start point sits left of the canvas, or coordinates computed from node positions that overflow a canvas sized too small for the laid-out graph.

Common situations: Auto-sized canvases too narrow for wide graphs (node x-offsets exceed cols); porting/customizing `DrawGraph` layout math so boundary nodes land at x == cols; mirrored layouts producing negative x for leftmost labels.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/dcbe9d4a36754c7f. Report an issue: GitHub.