{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/dspy-compiling-declarative-language-model","title":"DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines","arxiv_id":"2310.03714","date":"2023-10-05","proceeding":null,"authors":["Omar Khattab","Arnav Singhvi","Paridhi Maheshwari","Zhiyuan Zhang","Keshav Santhanam","Sri Vardhamanan","Saiful Haq","Ashutosh Sharma","Thomas T. Joshi","Hanna Moazam","Heather Miller","Matei Zaharia","Christopher Potts"],"abstract":"The ML community is rapidly exploring techniques for prompting language models (LMs) and for stacking them into pipelines that solve complex tasks. Unfortunately, existing LM pipelines are typically implemented using hard-coded \"prompt templates\", i.e. lengthy strings discovered via trial and error. Toward a more systematic approach for developing and optimizing LM pipelines, we introduce DSPy, a programming model that abstracts LM pipelines as text transformation graphs, i.e. imperative computational graphs where LMs are invoked through declarative modules. DSPy modules are parameterized, meaning they can learn (by creating and collecting demonstrations) how to apply compositions of prompting, finetuning, augmentation, and reasoning techniques. We design a compiler that will optimize any DSPy pipeline to maximize a given metric. We conduct two case studies, showing that succinct DSPy programs can express and optimize sophisticated LM pipelines that reason about math word problems, tackle multi-hop retrieval, answer complex questions, and control agent loops. Within minutes of compiling, a few lines of DSPy allow GPT-3.5 and llama2-13b-chat to self-bootstrap pipelines that outperform standard few-shot prompting (generally by over 25% and 65%, respectively) and pipelines with expert-created demonstrations (by up to 5-46% and 16-40%, respectively). On top of that, DSPy programs compiled to open and relatively small LMs like 770M-parameter T5 and llama2-13b-chat are competitive with approaches that rely on expert-written prompt chains for proprietary GPT-3.5. DSPy is available at https://github.com/stanfordnlp/dspy","url_abs":"https://arxiv.org/abs/2310.03714v1","url_pdf":"https://arxiv.org/pdf/2310.03714v1.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"dspy-compiling-declarative-language-model","repo_url":"https://github.com/stanfordnlp/dspy","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}},{"paper_slug":"dspy-compiling-declarative-language-model","repo_url":"https://github.com/stanfordnlp/dsp","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}},{"paper_slug":"dspy-compiling-declarative-language-model","repo_url":"https://github.com/codelion/optillm","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":0,"framework":"pytorch","reach":null}],"tasks":[{"task_slug":"language-modeling","task_name":"Language Modeling"},{"task_slug":"language-modelling","task_name":"Language Modelling"},{"task_slug":"math","task_name":"Math"},{"task_slug":"question-answering","task_name":"Question Answering"}],"methods":[{"method_slug":"adafactor","method_name":"Adafactor"},{"method_slug":"adam","method_name":"Adam"},{"method_slug":"attention","method_name":"Attention"},{"method_slug":"attention-dropout","method_name":"Attention Dropout"},{"method_slug":"bpe","method_name":"BPE"},{"method_slug":"cosine-annealing","method_name":"Cosine Annealing"},{"method_slug":"dense-connections","method_name":"Dense Connections"},{"method_slug":"dropout","method_name":"Dropout"},{"method_slug":"gpt-3","method_name":"GPT-3"},{"method_slug":"glu","method_name":"Gated Linear Unit"},{"method_slug":"inverse-square-root-schedule","method_name":"Inverse Square Root Schedule"},{"method_slug":"layer-normalization","method_name":"Layer Normalization"},{"method_slug":"linear-layer","method_name":"Linear Layer"},{"method_slug":"linear-warmup-with-cosine-annealing","method_name":"Linear Warmup With Cosine Annealing"},{"method_slug":"multi-head-attention","method_name":"Multi-Head Attention"},{"method_slug":"residual-connection","method_name":"Residual Connection"},{"method_slug":"sentencepiece","method_name":"SentencePiece"},{"method_slug":"softmax","method_name":"Softmax"},{"method_slug":"t5","method_name":"T5"},{"method_slug":"weight-decay","method_name":"Weight Decay"}],"datasets_introduced":[],"methods_introduced":[],"results":[{"leaderboard":"/sota/question-answering-on-fever","task":"Question Answering","dataset":"FEVER","model":"DSP","rank_in_archive_order":4,"of":8,"metrics":{"EM":"62.2"},"uses_additional_data":false},{"leaderboard":"/sota/question-answering-on-webquestions","task":"Question Answering","dataset":"WebQuestions","model":"DSP","rank_in_archive_order":3,"of":37,"metrics":{"EM":"59.4"},"uses_additional_data":false}],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2310.03714","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2310.03714"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/stanfordnlp/dsp","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/stanfordnlp/dspy","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/codelion/optillm","reach":null}],"summary":{"ran":3,"unverified":4},"by_repo_kind":{"listed":{"samples":7,"ran":3,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"8823c5b244127289","entry":"deduplicate","repo":"stanfordnlp/dsp","repo_kind":"listed","path":"dspy/dsp/utils/utils.py","file_url":"https://github.com/stanfordnlp/dsp/blob/HEAD/dspy/dsp/utils/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"8823c5b244127289"}},{"code_sha256_prefix":"f3fc668675b416fa","entry":"flatten","repo":"stanfordnlp/dsp","repo_kind":"listed","path":"dspy/dsp/utils/utils.py","file_url":"https://github.com/stanfordnlp/dsp/blob/HEAD/dspy/dsp/utils/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"f3fc668675b416fa"}},{"code_sha256_prefix":"a1443403bbc0e0a0","entry":"timestamp","repo":"stanfordnlp/dsp","repo_kind":"listed","path":"dspy/dsp/utils/utils.py","file_url":"https://github.com/stanfordnlp/dsp/blob/HEAD/dspy/dsp/utils/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"a1443403bbc0e0a0"}},{"code_sha256_prefix":"47676c1faa234786","entry":"DPR_tokenize","repo":"stanfordnlp/dsp","repo_kind":"listed","path":"dspy/dsp/utils/dpr.py","file_url":"https://github.com/stanfordnlp/dsp/blob/HEAD/dspy/dsp/utils/dpr.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"47676c1faa234786"}},{"code_sha256_prefix":"f7dd796d65e2945a","entry":"find_data_errors_completion","repo":"stanfordnlp/dsp","repo_kind":"listed","path":"dspy/clients/utils_finetune.py","file_url":"https://github.com/stanfordnlp/dsp/blob/HEAD/dspy/clients/utils_finetune.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"f7dd796d65e2945a"}},{"code_sha256_prefix":"750c77c03f65dc36","entry":"has_answer","repo":"stanfordnlp/dsp","repo_kind":"listed","path":"dspy/dsp/utils/dpr.py","file_url":"https://github.com/stanfordnlp/dsp/blob/HEAD/dspy/dsp/utils/dpr.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"750c77c03f65dc36"}},{"code_sha256_prefix":"007531ee995976cc","entry":"locate_answers","repo":"stanfordnlp/dsp","repo_kind":"listed","path":"dspy/dsp/utils/dpr.py","file_url":"https://github.com/stanfordnlp/dsp/blob/HEAD/dspy/dsp/utils/dpr.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"007531ee995976cc"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}