Automatic Prompt Optimization for Knowledge Graph Construction: Insights from an Empirical Study

Nandana Mihindukulasooriya, Niharika S. D'Souza, Faisal Chowdhury, Horst SamulowitzarXiv 2025

TL;DR

Handcrafting prompts for LLM-based Knowledge Graph construction is a tedious, brittle, and labor-intensive process. This paper presents the first comprehensive empirical study applying Automatic Prompt Optimization (APO) frameworks—specifically DSPy, APE, and TextGrad—to the task of triple extraction. The authors demonstrate that automated tools can generate human-like prompts that significantly boost extraction performance, especially as schema complexity and context lengths scale up.

What Problem Does This Paper Address?

Knowledge Graphs (KGs) represent networks of real-world entities and their interrelations, serving as the backbone for semantic search, reasoning, and Retrieval-Augmented Generation (RAG) systems. Building a KG from unstructured text relies heavily on "triple extraction" (identifying subject-relation-object triplets).

While Large Language Models (LLMs) excel at this task, getting them to do it accurately requires precise prompt engineering. Unfortunately, manual prompt design is more of an art than a science; it is highly sensitive to minor wording changes and often breaks when switching to a different LLM or updating an existing one. This paper addresses this bottleneck by exploring whether Automatic Prompt Optimization (APO)—algorithms that systematically search for, evaluate, and refine prompts using data—can reliably automate and improve prompt design for closed-domain triple extraction.

Methods

The authors systematically evaluate three prominent APO frameworks with distinct optimization flavors:

  1. DSPy (with MIPROv2): Frames prompting as a declarative program. It uses Bayesian optimization to jointly search for the best combination of instructions and few-shot examples.
  2. APE (Automated Prompt Engineer): Generates a pool of instruction candidates directly from input-output demonstrations and selects the top performer based on evaluation scores.
  3. TextGrad: Treats text as a variable and utilizes natural language feedback from an LLM as "gradients" to iteratively refine prompts via textual gradient descent.

These frameworks were tested across six different prompting strategies, ranging from direct prediction to Chain-of-Thought (CoT) reasoning and multi-step Extract-Critique-Refine (ECR) pipelines.

Table 1: Different prompting strategies for applying APO.

The optimized prompts were evaluated using several powerful open-source LLMs, including Llama 3.3-70B, DeepSeek V3, Qwen2.5-72B, Mistral-8x22B, Phi-4, and Granite-3.2-8B.

Datasets

The empirical study utilizes two distinct relation extraction benchmarks:

  • SynthIE: A dataset containing 888 relation types, generated using subgraphs of Wikidata paired with a synthetic text generation strategy.
  • REBEL: A larger benchmark containing 1,079 relation types, created by aligning Wikipedia abstracts to Wikidata triples and filtering them using a Natural Language Inference (NLI) model.

Key Insights and Ideas

  • APO Shines in Hard Scenarios: The value of automatic prompt optimization increases with task difficulty. The most substantial performance gains over baseline prompts occurred when schema complexity (the number of allowed relations to choose from) scaled up from 100 to 800, and when input text lengths were increased up to 10x.
  • Inference LLM Matters More Than Generation LLM: Interestingly, the choice of the LLM executing the final task at inference time is far more critical than the LLM used to optimize the prompt. Prompts optimized by smaller models (like Granite-3.2-8B) performed remarkably well when executed by larger models (like Llama 3.3-70B).
  • Cross-Dataset Transfer is a Major Hurdle: Prompts optimized on one dataset (e.g., REBEL) do not transfer effectively to another (e.g., SynthIE). This domain/distribution shift resulted in only marginal gains (~1% F1 increase) compared to the substantial improvements (~8% or more) achieved when training and testing on the same dataset.
  • Text Coherence Impacts Extraction: When expanding context lengths, augmenting the text with "adversarial" (highly dissimilar/unrelated) text caused a much harsher drop in extraction recall than adding random or related text, proving that text coherence heavily influences an LLM's extraction capability.

Key Results

  • Consistent Performance Boosts: Every single prompting strategy benefited from automatic optimization. For example, Chain-of-Thought focusing solely on triples (CoT (T)) achieved the highest overall triple extraction F1 score of 0.73 (up from a baseline of 0.64).
  • Massive Pipeline Improvements: The multi-step Extract, Critique, and Refine pipeline (ECR (E-R-T)) saw the largest relative jump, boosting triple extraction F1 by +16% (from 0.53 to 0.69).
  • Optimizer Comparison: Overall, DSPy maintained a slight performance edge over APE and TextGrad. Furthermore, TextGrad was noted as the most computationally expensive and slowest framework to train, requiring at least two API calls per training instance per epoch.
Figure 2: An example of an automatically optimized prompt using DSPy with the Llama 3.3-70B model and the SynthIE dataset as training/validation data.

Limitations and Future Work

The primary limitation identified is the lack of cross-dataset transferability, meaning users must re-run the prompt optimization process whenever they shift to a text corpus with a different domain or distribution. Additionally, the computational cost (measured in total LLM API calls) can be steep for gradient-based text optimizers.

For future work, the authors plan to explore cross-lingual prompt optimization to support multilingual knowledge graph construction. They also aim to develop interactive human-in-the-loop prompt optimization, allowing human engineers to inject domain expertise or preferences into the automated optimization loop.

Summary updated

BibTeX
@misc{mihindukulasooriya2025automatic,
  author = {Nandana Mihindukulasooriya and Niharika S. D'Souza and Faisal Chowdhury and Horst Samulowitz},
  title = {{Automatic Prompt Optimization for Knowledge Graph Construction: Insights from an Empirical Study}},
  year = {2025},
  eprint = {2506.19773},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.19773},
}
Figure