Skip to content

createAgent examples fail to save graph PNGs on Windows #11464

Description

@dvd233

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is present on the latest main branch.

Example Code

The createAgent examples derive their graph PNG output path with new URL(import.meta.url).pathname. This self-contained reproduction exercises the same URL-to-path conversion without calling an LLM:

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";

const target = path.join(os.tmpdir(), "langchain-example-path-repro.png");
const currentFilePath = new URL(pathToFileURL(target)).pathname;

console.log({ currentFilePath, resolved: path.resolve(currentFilePath) });
await fs.writeFile(currentFilePath, "png data");

On Windows this prints a URL pathname and then resolves it as a different filesystem path:

{
  currentFilePath: '/C:/Users/<user>/AppData/Local/Temp/langchain-example-path-repro.png',
  resolved: 'C:\\C:\\Users\\<user>\\AppData\\Local\\Temp\\langchain-example-path-repro.png'
}

Error: ENOENT: no such file or directory, open 'C:\C:\Users\<user>\AppData\Local\Temp\langchain-example-path-repro.png'

Error Message and Stack Trace (if applicable)

ENOENT: no such file or directory, open 'C:\C:\Users\<user>\...\langchain-example-path-repro.png'

Description

Fifteen examples under examples/src/createAgent/ currently use this pattern before calling fs.writeFile:

const currentFilePath = new URL(import.meta.url).pathname;
const outputPath = currentFilePath.replace(/\.ts$/, ".png");
await fs.writeFile(outputPath, await agent.drawMermaidPng());

On POSIX, a file URL's pathname is usually a usable absolute path. On Windows, it starts with /C:/; Node resolves that as C:\C:\..., so the examples fail after the agent run instead of saving their visualization beside the source file. Percent-encoded characters in paths (for example spaces) are also left encoded by .pathname.

Expected: each example saves its graph PNG next to its TypeScript source on every supported Node platform.

Actual: all 15 affected examples fail with ENOENT on Windows when they attempt to write the PNG.

Node already provides the cross-platform conversion used elsewhere in this repository:

import { fileURLToPath } from "node:url";

const currentFilePath = fileURLToPath(import.meta.url);

I found no existing issue or PR for this specific example-path bug. If maintainers agree with the fix direction, I am happy to update the affected examples and run the examples workspace build and formatting checks.

System Info

langchainjs main: 53dbfc2fa033250fe7b711d7c3c8f6e7bfea9216
OS: Microsoft Windows 11 64-bit, build 26200
Node: v24.15.0
pnpm: 11.19.0 (the reproduction itself only uses Node built-ins)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions