🏋️ Practice Your Signature Skills
These exercises will help you master DSPy signatures. Start with the basics and work up to more complex designs.
Tip: Try to complete each exercise before looking at the solutions. The best way to learn is by doing!
Inline Signature Basics
Task: Create inline signatures for the following tasks and test them with sample inputs.
Simple Translation
Create an inline signature that takes text and translates it to Spanish.
Headline Generator
Create an inline signature that takes an article and generates a catchy headline.
Fact Checker
Create an inline signature with two inputs (claim, context) that outputs whether the claim is supported.
Expected Skills:
- Using
dspy.Predict()with string signatures - Handling single and multiple inputs
- Accessing output fields
Your First Class-Based Signature
Task: Convert the following inline signature to a class-based signature with proper documentation:
# Convert this inline signature:
qa = dspy.Predict("context, question -> answer")
Requirements:
- Create a class named
ContextQA - Add a descriptive docstring
- Include type hints for all fields
- Add descriptions using the
desc=parameter - Test with a sample question
Multi-Output Signature
Task: Design a signature for comprehensive email analysis.
Requirements:
- Input:
email_text(the full email content) - Outputs:
subject- What the email is aboutsender_intent- What the sender wants (request, inform, complaint, etc.)urgency- How urgent (low, medium, high)action_required- Boolean indicating if action is neededsuggested_response- A brief suggested reply
Test your signature with this email:
Hi Team,
The client meeting has been moved to tomorrow at 3 PM instead of Thursday.
Please confirm your availability ASAP as we need to finalize attendees.
This is critical - they're making a decision on our proposal next week.
Thanks,
Sarah
Using Literal Types
Task: Create a content moderation signature using
Literal types for constrained outputs.
Requirements:
- Input:
content- Text to moderate - Outputs:
category- One of: "safe", "mild", "sensitive", "harmful"flags- List of issues detected (violence, hate_speech, adult_content, spam, misinformation)confidence- Float between 0.0 and 1.0requires_review- Boolean for human reviewexplanation- Reasoning for the classification
Test with various content types:
- "Check out this amazing recipe for chocolate cake!"
- "Breaking: Aliens confirmed to live among us!" (misinformation)
- A legitimate news headline of your choice
Signature Collection for a Blog Platform
Task: Design a complete set of signatures for a blog platform's AI features.
Create these signatures:
GenerateBlogPost
Takes a topic and key points, generates a complete blog post with title, introduction, body sections, and conclusion.
SEOOptimizer
Takes a blog post and generates SEO metadata: meta title, meta description, keywords, and suggested URL slug.
SocialMediaPosts
Takes a blog post and generates social media content: tweet (280 chars), LinkedIn post, and Instagram caption.
ContentAnalyzer
Takes a blog post and outputs: reading time, difficulty level, target audience, topics covered, and improvement suggestions.
Bonus Challenge: Create a simple script that uses all four signatures in sequence to demonstrate a complete content pipeline.
💡 Exercise Tips
Write Clear Docstrings
The docstring is your main instruction to the LM - make it specific!
Use Appropriate Types
Choose types that match your expected output format (str, list, bool, Literal)
Add Constraints in Descriptions
Use descriptions to specify formats, lengths, and valid values
Test with Edge Cases
Try unusual inputs to see how your signatures handle them
Great Work!
Completed the exercises? Check your solutions!