TaskWeaver: A Code-First Agent Framework

Bo Qiao, Liqun Li, Xu Zhang, Shilin He, Yu Kang, Chaoyun Zhang, Fangkai Yang, Hang Dong, Jue Zhang, Lu Wang, Minghua Ma, Pu Zhao, Si Qin, Xiaoting Qin, Chao Du, Yong Xu, Qingwei Lin, Saravan Rajmohan, Dongmei ZhangarXiv 2023 · DOI

Abstract

TaskWeaver is a novel framework for building LLM-powered autonomous agents that prioritizes executable code as the primary medium of action. By converting user requests into Python snippets and supporting native data structures like pandas DataFrames, it enables agents to handle complex, domain-specific data analytics tasks that traditional "string-passing" frameworks struggle to manage.

Introduction

While Large Language Models (LLMs) are excellent conversationalists, turning them into reliable "agents" that can perform complex data tasks is difficult. Existing frameworks often treat tools as black boxes and pass information between them as strings or JSON objects. This creates significant hurdles: data bottlenecks due to context window limits, inflexibility with ad-hoc requests, and a gap in injecting domain-specific business logic. TaskWeaver addresses these by adopting a "code-first" approach, where the agent acts as a dynamic Python programmer.

System Architecture

The TaskWeaver architecture is divided into two primary roles: the Planner and the Code Interpreter (CI).

The TaskWeaver architecture consists of a Planner for high-level task decomposition and a Code Interpreter for generating and executing Python code.

The Planner

The Planner acts as the system's entry point. It interacts with the user, breaks down complex requests into a high-level plan of sub-tasks, and manages "self-reflection" to adjust the plan if execution fails.

The Code Interpreter (CI)

This component consists of a Code Generator (CG) and a Code Executor (CE). The CG writes Python code to accomplish sub-tasks, treating user-defined plugins as library functions. The CE runs this code in a stateful environment (similar to a Jupyter Notebook), allowing variables and DataFrames to persist across conversation rounds.

Safety and Memory

To ensure security, TaskWeaver uses Abstract Syntax Tree (AST) verification before execution and runs code within isolated Docker containers. Additionally, an "Experience Memory" feature allows the agent to store "tips" from past interactions, helping it learn user preferences over time.

Evaluation

TaskWeaver was evaluated using a mix of established benchmarks and new test cases, including DS-1000 (1,000 data science problems from StackOverflow) and InfiAgent-DABench (focused on data analytics).

An example interaction showing TaskWeaver pulling data from a database, performing anomaly detection, and visualizing results.

Key Results

Evaluation comparing GPT-3.5 and GPT-4 showed a significant performance gap. On the authors' custom Eval-Cases, GPT-4 achieved a normalized score of 0.87, while GPT-3.5 scored 0.42. On InfiAgent-DABench, TaskWeaver with GPT-4 reached a score of 0.88, demonstrating high proficiency in data analysis and CSV handling.

Discussion and Insights

A primary insight of TaskWeaver is that native data structures are essential for data science. By working directly with pandas DataFrames, the framework avoids information loss associated with string conversion. Furthermore, the two-phase planning process—generating a plan and then refining it by merging sub-tasks—reduces LLM calls and improves efficiency.

However, limitations remain. LLM variability can lead to inconsistent results, and the cost of using high-performing models like GPT-4 is substantial. Future work will focus on multi-agent collaboration and integrating specialized open-source models to optimize the cost-to-performance ratio.

Conclusion

TaskWeaver represents a shift toward more robust, code-centric agent frameworks. By treating code as the first-class citizen for action and data handling, it bridges the gap between conversational AI and practical data engineering.

Summary updated

BibTeX
@misc{qiao2023taskweaver,
  author = {Bo Qiao and Liqun Li and Xu Zhang and Shilin He and Yu Kang and Chaoyun Zhang and Fangkai Yang and Hang Dong and Jue Zhang and Lu Wang and Minghua Ma and Pu Zhao and Si Qin and Xiaoting Qin and Chao Du and Yong Xu and Qingwei Lin and Saravan Rajmohan and Dongmei Zhang},
  title = {{TaskWeaver: A Code-First Agent Framework}},
  year = {2023},
  eprint = {2311.17541},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2311.17541},
}
Figure