Skip to content

feat(core): Add EChartsOutputParser for structured chart generation #36735

@slepybear

Description

@slepybear

Checked other resources

  • This is a feature request, not a bug report or usage question.
  • I added a clear and descriptive title that summarizes the feature request.
  • I used the GitHub search to find a similar feature request and didn't find it.
  • I checked the LangChain documentation and API reference to see if this feature already exists.
  • This is not related to the langchain-community package.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Feature Description

I would like to propose adding an EChartsOutputParser to langchain-core that parses LLM output into a valid ECharts option configuration object.

ECharts is one of the most popular open-source charting libraries, widely used in both English and Chinese-speaking developer communities. It uses a declarative JSON-based option object to describe charts, making it a natural fit for LLM-driven chart generation.

The parser would:

  1. Parse LLM text output (including markdown code blocks) into a JSON object representing an ECharts option
  2. Validate that the parsed object contains the required series property (must be an array)
  3. Provide format instructions that guide the LLM to produce valid ECharts option structures, with examples for common chart types (bar, line, pie, scatter, etc.)
  4. Support streaming by inheriting from JsonOutputParserBaseCumulativeTransformOutputParser
  5. Optionally accept a Pydantic model for more precise schema-driven format instructions

Use Case

When building LLM-powered data analysis or reporting applications, users often want to generate interactive charts from natural language descriptions. For example:

  • A data analyst asks an LLM agent to "create a bar chart showing quarterly sales" and expects a renderable chart configuration
  • A reporting tool generates ECharts configurations from natural language summaries
  • A dashboard builder uses an LLM to suggest chart types and configurations based on a dataset

Currently, users must manually parse LLM output as JSON and validate the ECharts option structure themselves. An EChartsOutputParser would streamline this workflow by:

  • Providing clear format instructions so the LLM produces valid ECharts options
  • Validating the series property is present and correctly structured
  • Supporting streaming for real-time chart rendering
  • Raising descriptive OutputParserException errors when the output is invalid, helping the LLM self-correct

Proposed Solution

Add an EChartsOutputParser class to langchain_core/output_parsers/echarts.py that:

  • Inherits from JsonOutputParser (which already handles JSON parsing, markdown code block extraction, and streaming)
  • Overrides parse_result() to add ECharts-specific validation:
    • The result must be a JSON object (dict)
    • The series property must exist and be an array
  • Overrides get_format_instructions() to return ECharts-specific format guidance with:
    • Description of the ECharts option structure
    • List of common top-level properties (title, tooltip, legend, xAxis, yAxis, series, dataset, grid, color)
    • Examples for bar chart and pie chart
    • Optional Pydantic schema when provided
  • Sets _type property to "echarts"
  • Exports from langchain_core.output_parsers

Example usage:

from langchain_core.output_parsers import EChartsOutputParser

parser = EChartsOutputParser()
result = parser.parse('''
{
    "title": {"text": "Sales Report"},
    "xAxis": {"type": "category", "data": ["Q1", "Q2", "Q3", "Q4"]},
    "yAxis": {"type": "value"},
    "series": [{"type": "bar", "data": [120, 200, 150, 80]}]
}
''')
# result is a dict that can be directly used as an ECharts option

With a chain:

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import EChartsOutputParser

prompt = ChatPromptTemplate.from_template(
    "Generate an ECharts chart configuration for: {query}"
)
parser = EChartsOutputParser()
chain = prompt | model | parser
chart_option = chain.invoke({"query": "Monthly sales as a bar chart"})

Alternatives Considered

  1. Using JsonOutputParser directly: Users could use JsonOutputParser and manually validate the ECharts structure, but this requires custom validation code and does not provide ECharts-specific format instructions to the LLM.

  2. Placing in langchain-classic instead of langchain-core: Since ECharts is a specific library, one could argue it belongs in langchain-classic. However, the parser has zero external dependencies (it only validates JSON structure, not against the ECharts runtime), and output parsers for common structured formats (JSON, Pydantic, XML, List) already live in langchain-core. The ECharts option is essentially a JSON schema with domain-specific validation, similar to how PydanticOutputParser adds Pydantic validation on top of JSON parsing.

  3. Creating a separate partner package: This would add unnecessary overhead for a lightweight parser with no external dependencies.

Additional Context

  • ECharts has 64k+ GitHub stars and is the most popular charting library in the Chinese developer ecosystem
  • The parser adds no new dependencies — it only uses existing langchain-core utilities (parse_json_markdown, OutputParserException, etc.)
  • The implementation is ~200 lines of code including docstrings and format instructions
  • Full test coverage with 20 unit tests covering parsing, validation, streaming, format instructions, and error handling
  • I have a working implementation ready to submit as a PR once this issue is approved

Metadata

Metadata

Assignees

No one assigned

    Labels

    core`langchain-core` package issues & PRsexternal

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions