CoCo: Code as CoT for Text-to-Image Preview and Rare Concept Generation

Haodong Li, Chunmei Qing, Huanyu Zhang, Dongzhi Jiang, Yihang Zou, Hongbo Peng, Dingming Li, Yuhong Dai, ZePeng Lin, Juanxi Tian, Yi Zhou, Siqi Dai, Jingwei Wu · OpenReview

TL;DR

This paper introduces CoCo, a novel approach to text-to-image generation that uses executable code as a form of Chain-of-Thought (CoT) reasoning. By generating code to define scene structure and then refining a draft image, CoCo achieves more precise control over complex layouts and text rendering, outperforming existing methods.

What Problem Does This Paper Address?

Current text-to-image (T2I) generation models, even those using Chain-of-Thought (CoT) reasoning, often struggle with prompts requiring precise spatial arrangements, detailed visual elements, and dense textual content. This is because natural language planning, while helpful, is too abstract to specify exact layouts, object relationships, or textual accuracy. This limitation is particularly apparent in domains like scientific diagrams or complex infographics. The paper aims to bridge this gap by introducing a more concrete and verifiable reasoning process.

Methods

CoCo employs a three-stage pipeline:

  1. Code Generation: The model takes a text prompt and generates executable code that explicitly defines the scene's structural layout, object placement, and textual elements. This code acts as a deterministic intermediate representation of the reasoning process.
  2. Draft Image Rendering: The generated code is executed in a secure, sandboxed environment to produce a draft image. This draft serves as a visual scaffold, concretely instantiating the planned structure.
  3. Draft-Guided Refinement: The draft image is then fed back into the model, along with the original prompt, for fine-grained image editing. This stage enhances visual fidelity and realism while preserving the structural integrity established by the draft.

The core innovation is using executable code as the "Chain-of-Thought," moving beyond abstract natural language to a precise, verifiable planning mechanism.

Datasets

  • CoCo-10K: A newly constructed dataset designed to train CoCo. It contains over 10,000 instances and is organized into two categories:
  • Text-Code Pairs: For learning executable code generation.
  • Text-Draft Image–Final Image Triplets: For learning both draft construction and draft-guided refinement.
  • StructT2IBench: Used for evaluating performance on structured image synthesis tasks.
  • OneIG-Bench: Used for evaluating text rendering capabilities (English and Chinese).
  • LongText-Bench: Used for evaluating the rendering of long textual content within images.
  • StructVisuals: Used for caption annotations to preserve visual perception ability during training.

Key Insights and Ideas

  • Code as a Precise Reasoning Medium: The paper demonstrates that executable code, unlike natural language, can precisely encode spatial layouts, structural constraints, and textual placements, leading to more controllable and accurate image generation.
  • Draft as a Verifiable Scaffold: Generating a draft image from code provides a concrete, observable representation of the model's reasoning, allowing for targeted refinements rather than relying solely on abstract planning.
  • Two-Stage Generation is Key: The combination of structured draft generation (via code) and subsequent draft-guided refinement is crucial for achieving both precise layouts and high-fidelity visual outputs.
  • Code Executability is Essential: The study highlights that for code-based reasoning to be effective, the generated code must be executable. Fine-tuning with text-code supervision is vital for achieving this.

Key Results

CoCo significantly outperforms direct generation and other CoT-based methods on various benchmarks:

  • StructT2IBench: Achieves an overall accuracy of 73.52%, a +68.83% improvement over direct generation and outperforming the best baseline (GPT-Image) by a large margin. It excels in tasks requiring precise layouts like charts, graphs, math figures, and tables.
  • OneIG-Bench (Text Rendering): Achieves an overall score of 0.853, outperforming compared methods.
  • LongText-Bench (Long Text Rendering): Achieves an overall score of 0.754, demonstrating strong performance in rendering extended textual content.
  • Generalization: CoCo shows adaptive aspect ratio generation based on prompt semantics, indicating it learns to treat canvas size and layout as programmable parameters rather than memorizing fixed configurations.

Limitations and Future Work

  • Limitations: The paper notes that initial pilot studies showed limitations in Bagel's ability to generate executable code and its tendency to ignore draft semantics. While CoCo addresses this, the reliance on a powerful MLLM and image-editing models for dataset construction is a significant undertaking.
  • Future Work: The paper suggests that the code-based reasoning paradigm is a promising direction for precise, controllable, and structured text-to-image generation. Further exploration of this approach for more complex visual tasks and broader applications is implied. The adaptive aspect ratio generation also points to future work in dynamic canvas configuration.

Summary updated

BibTeX
@misc{li2026coco,
  author = {Haodong Li and Chunmei Qing and Huanyu Zhang and Dongzhi Jiang and Yihang Zou and Hongbo Peng and Dingming Li and Yuhong Dai and ZePeng Lin and Juanxi Tian and Yi Zhou and Siqi Dai and Jingwei Wu},
  title = {{CoCo: Code as CoT for Text-to-Image Preview and Rare Concept Generation}},
  year = {2026},
  eprint = {2603.08652},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2603.08652},
}
Figure