From Hand-Crafted Prompts to Structured Programs
Manual prompt engineering works fine in a notebook. In production software it becomes fragile, hard to version, and expensive to maintain. Brett Kennedy returned to The Real Python Podcast to show a different path: treat prompts as code that can be declared, compiled, and tuned automatically.
His new book, Building LLM Applications with DSPy, centers on the Stanford project that reframes language-model interactions as ordinary Python functions. Instead of writing long strings of instructions, you define a signature that names the inputs and the expected outputs. DSPy then generates the actual prompt text, runs evaluations, and optimizes the wording for the model and task you chose.
How Declarative Signatures Work
A signature looks like a type annotation. You state what data goes in and what structured result should come out. DSPy handles the rest: few-shot examples, chain-of-thought scaffolding, and iterative improvement. Because the prompt is now a compiled artifact, you can re-target the same program to a different model or temperature setting without rewriting the human-readable instructions.
Kennedy walks through the practical loop: write the signature, supply a small metric and a training set, then call the optimizer. The system searches for better prompts the same way a compiler searches for better machine code. The result is a prompt that is both more reliable and easier to keep under source control.
Why Production Teams Care
When large language models sit inside shipping products, every prompt change is a deployment risk. DSPy moves that risk into a testable, reproducible pipeline. Teams can version the signatures, run continuous evaluation, and measure regression the same way they already measure unit tests. The podcast also touches on how the approach fits into larger compound systems that combine retrieval, tools, and multiple models.
The episode ends with a clear invitation: stop treating prompts as one-off art and start treating them as programmable components. For Python developers already comfortable with type hints and pipelines, the mental model feels natural. The tooling is still young, yet the direction is unmistakable. Prompt engineering is becoming software engineering.
Listeners who want the full technical walkthrough can find the episode and Kennedy’s book links on the Real Python site.