n8n - 💡(How to fix) Fix `POST /rest/ai/sessions` returns 500 when loading code-builder sessions (`invalid input syntax for type uuid`) [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Fixed

Code Example

// packages/@n8n/ai-workflow-builder.ee/src/utils/thread-id.ts
return agentType === 'code-builder' ? `${baseId}-code` : baseId;

---

invalid input syntax for type uuid: "3162e4af-2f84-4fc0-84a1-ab892333a9d0-code"

---

{
  "workflowId": "<workflow-id>",
  "codeBuilder": false
}

---

{
  "code": 500,
  "message": "invalid input syntax for type uuid: \"<user-uuid>-code\""
}

---

const match = threadId.match(/^workflow-(.+)-user-(.+)$/);
// userId capture group incorrectly includes "-code"

---

const normalized = threadId.endsWith('-code') ? threadId.slice(0, -5) : threadId;
const match = normalized.match(/^workflow-(.+)-user-(.+)$/);
RAW_BUFFERClick to expand / collapse

Bug Description

Loading AI Workflow Builder chat sessions fails with HTTP 500 when the backend uses a code-builder thread ID.

SessionManagerService.generateThreadId() appends a -code suffix for code-builder sessions:

// packages/@n8n/ai-workflow-builder.ee/src/utils/thread-id.ts
return agentType === 'code-builder' ? `${baseId}-code` : baseId;

Example thread ID:

workflow-RuZf36C7AWRmbiux-user-3162e4af-2f84-4fc0-84a1-ab892333a9d0-code

WorkflowBuilderSessionRepository.parseThreadId() does not strip that suffix. It parses userId as:

3162e4af-2f84-4fc0-84a1-ab892333a9d0-code

The workflow_builder_session.userId column is type uuid, so PostgreSQL rejects the query:

invalid input syntax for type uuid: "3162e4af-2f84-4fc0-84a1-ab892333a9d0-code"

AiWorkflowBuilderAgentService.getSessions() always passes agentType: 'code-builder', so this path is hit whenever sessions are loaded from persistent storage (not only when the UI experiment flag is on).

To Reproduce

  1. Run n8n with PostgreSQL and AI Builder enabled (feat:aiBuilder).

  2. Ensure workflow builder session persistence is active (default when workflow_builder_session table exists).

  3. Open a workflow in the editor and open the AI Workflow Builder panel (or trigger session load).

  4. In the browser, observe:

POST /rest/ai/sessions

Request body:

{
  "workflowId": "<workflow-id>",
  "codeBuilder": false
}

Response is 500 with a message similar to:

{
  "code": 500,
  "message": "invalid input syntax for type uuid: \"<user-uuid>-code\""
}

Note: codeBuilder: false in the request does not prevent the backend from using a code-builder thread ID today; see related notes in Debug Info.

Expected behavior

  • POST /rest/ai/sessions should return 200 with { sessions: [...] } (or an empty list if no session exists).
  • userId parsed from the thread ID must be a valid UUID matching the authenticated user.
  • Code-builder thread IDs (…-code) should be handled consistently between session ID generation and DB lookup.

Debug Info

Affected code

FileIssue
packages/cli/src/modules/workflow-builder/workflow-builder-session.repository.tsparseThreadId() regex does not handle -code suffix
packages/@n8n/ai-workflow-builder.ee/src/utils/thread-id.tsAppends -code for agentType === 'code-builder'
packages/@n8n/ai-workflow-builder.ee/src/ai-workflow-builder-agent.service.tsgetSessions() always calls getSessions(..., 'code-builder')
packages/@n8n/api-types/src/dto/ai/ai-session-retrieval-request.dto.tsDTO has only workflowId; codeBuilder from frontend is ignored

Thread ID format

  • Standard: workflow-{workflowId}-user-{userId}
  • Code-builder: workflow-{workflowId}-user-{userId}-code

Repository parse logic (current)

const match = threadId.match(/^workflow-(.+)-user-(.+)$/);
// userId capture group incorrectly includes "-code"

Entity

  • WorkflowBuilderSession.userId@Column({ type: 'uuid' })
    (packages/cli/src/modules/workflow-builder/workflow-builder-session.entity.ts)

Suggested fix

Strip -code before parsing, e.g.:

const normalized = threadId.endsWith('-code') ? threadId.slice(0, -5) : threadId;
const match = normalized.match(/^workflow-(.+)-user-(.+)$/);

Operating System

Ubuntu

n8n Version

2.21.4

Node.js Version

24.15.0

Database

PostgreSQL

Execution mode

main (default)

Hosting

self hosted

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

  • POST /rest/ai/sessions should return 200 with { sessions: [...] } (or an empty list if no session exists).
  • userId parsed from the thread ID must be a valid UUID matching the authenticated user.
  • Code-builder thread IDs (…-code) should be handled consistently between session ID generation and DB lookup.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING