How to Upgrade Dify to 1.9.1 and Resolve LLM Iterator Errors

This guide walks you through upgrading Dify using Docker Compose or source code deployment, running required migration commands, backing up data, and fixing the "Invalid context structure" error caused by iterator output changes in version 1.9.1, with detailed code snippets and troubleshooting steps.

Architect's Alchemy Furnace
Architect's Alchemy Furnace
Architect's Alchemy Furnace
How to Upgrade Dify to 1.9.1 and Resolve LLM Iterator Errors

Upgrade Steps

Important Notice After upgrading, run the following migration to convert existing datasource credentials: uv run flask transform-datasource-credentials

1. Docker Compose Deployment

Backup custom docker-compose.yaml (optional):

cd docker
cp docker-compose.yaml docker-compose.yaml.$(date +%s).bak

Pull the latest code from the main branch:

git checkout 1.9.1
git pull origin 1.9.1

Stop services (run in the Docker directory): docker compose down Backup data: tar -cvf volumes-$(date +%s).tgz volumes Upgrade services:

docker compose up -d

2. Source Code Deployment

Stop API server, worker, and web frontend.

Fetch the latest code from the release branch: git checkout 1.9.1 Update Python dependencies:

cd api
uv sync

Run migration scripts:

uv run flask db upgrade
uv run flask transform-datasource-credentials

After containers start, run the credential migration:

docker exec -it docker-api-1 uv run flask transform-datasource-credentials

Upgrade Issues

1. Knowledge base iterator output to LLM throws error: "Run failed: Invalid context structure: xxx"

1.1 Symptom The iterator output format changed after upgrade, causing the LLM node to reject the context.

1.2 Root Cause The LLM node expects either a string or an array of objects each containing a content key. After the upgrade, the iterator returns a nested array of objects, which lacks the required content field at the top level.

1.3 Fix Insert a code execution node between the iterator and the LLM node to flatten the nested array and ensure each object has a content key. Example Python code:

from typing import List

def main(output: List[List[str]]):
    flattened = [item for sublist in output for item in sublist]
    return {"result": " ".join(flattened)}

The change was introduced in Dify 1.9.1 due to workflow engine refactoring and new parallel mode support, which now collects each parallel job's output into an ordered list without automatically flattening it.

Until an official fix is released, manually flatten the iterator output in downstream code nodes as shown above.

DockerPythonLLMUpgradeDify
Architect's Alchemy Furnace
Written by

Architect's Alchemy Furnace

A comprehensive platform that combines Java development and architecture design, guaranteeing 100% original content. We explore the essence and philosophy of architecture and provide professional technical articles for aspiring architects.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.