Skip to content
Back to skills

2485 Prompt Extraction 571db0db

ASecurity

After optimizing your DSPy module, you need to extract the optimized prompt and apply it to your Haystack pipeline.

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
developmentpythonperformance

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 2485-prompt-extraction_571db0db --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 2485 Prompt Extraction 571db0db?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 2485 Prompt Extraction 571db0db
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-2485-prompt-extraction-571db0db/badge)](https://www.skillsdirectory.com/skills/tools-only-2485-prompt-extraction-571db0db)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# Extracting and Applying Optimized Prompts

## Phase 6: Build Optimized Haystack Pipeline

After optimizing your DSPy module, you need to extract the optimized prompt and apply it to your Haystack pipeline.

### Extract DSPy Prompt

```python
def extract_dspy_prompt(compiled_module):
    """Extract the optimized prompt from compiled DSPy module."""
    # Get the predictor's demos and instructions
    predictor = compiled_module.generate

    demos = getattr(predictor, 'demos', [])

    # Build prompt with few-shot examples
    prompt_parts = ["Answer questions using the provided context.\n"]

    for demo in demos:
        prompt_parts.append(f"Context: {demo.context}")
        prompt_parts.append(f"Question: {demo.question}")
        prompt_parts.append(f"Answer: {demo.answer}\n")

    prompt_parts.append("Context: {{context}}")
    prompt_parts.append("Question: {{question}}")
    prompt_parts.append("Answer:")

    return "\n".join(prompt_parts)

optimized_prompt = extract_dspy_prompt(compiled)
```

### Apply to Haystack Pipeline

```python
from haystack import Pipeline
from haystack.components.builders import PromptBuilder
from haystack.components.generators import OpenAIGenerator
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever

# Create new pipeline with optimized prompt
optimized_pipeline = Pipeline()
optimized_pipeline.add_component("retriever", InMemoryBM25Retriever(document_store=doc_store))
optimized_pipeline.add_component("prompt_builder", PromptBuilder(template=optimized_prompt))
optimized_pipeline.add_component("generator", OpenAIGenerator(model="gpt-4o-mini"))

optimized_pipeline.connect("retriever", "prompt_builder.context")
optimized_pipeline.connect("prompt_builder", "generator")
```

## Tips

- Truncate long context examples to stay within token limits
- Limit to 3-5 few-shot examples for best balance
- Test the Haystack pipeline thoroughly after prompt extraction
- Compare performance against baseline pipeline

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.