Prompt engineering in LangExtract involves crafting clear instructions and high-quality examples that guide the LLM’s extraction behavior. Unlike traditional NLP systems, LangExtract doesn’t require training data or model fine-tuning—just well-designed prompts.
Your prompt description and examples are the only inputs that control extraction behavior. Small changes can significantly impact results.
prompt = textwrap.dedent("""\ Extract characters, emotions, and relationships in order of appearance. Use exact text for extractions. Do not paraphrase or overlap entities. Provide meaningful attributes for each entity to add context.""")
prompt = textwrap.dedent("""\ Extract medication mentions as complete phrases including: - Drug name - Dosage (if present) - Route of administration (if present) - Frequency (if present) Example: "aspirin 81mg PO daily" not just "aspirin""")
LangExtract expects extractions in order of appearance by default:
prompt = textwrap.dedent("""\ Extract entities in the order they appear in the text. If an entity appears multiple times, list each occurrence separately.""")
prompt = textwrap.dedent("""\ Extract character names: - Include titles if part of the name (e.g., "Lady Juliet") - Skip pronouns ("he", "she") unless they're the only reference - Extract formal names on first mention, nicknames on subsequent mentions""")
The model learns more from what you show than what you tell:
examples = [ lx.data.ExampleData( text="ROMEO. But soft! What light through yonder window breaks? It is the east, and Juliet is the sun.", extractions=[ lx.data.Extraction( extraction_class="character", extraction_text="ROMEO", attributes={"emotional_state": "wonder"} ), lx.data.Extraction( extraction_class="emotion", extraction_text="But soft!", attributes={"feeling": "gentle awe"} ), lx.data.Extraction( extraction_class="relationship", extraction_text="Juliet is the sun", attributes={"type": "metaphor"} ), ] )]
prompt = textwrap.dedent("""\ Extract only information explicitly stated in the text. Do not infer or add information from external knowledge.""")examples = [ lx.data.ExampleData( text="Lady Juliet gazed longingly at the stars", extractions=[ lx.data.Extraction( extraction_class="character", extraction_text="Lady Juliet", attributes={"emotional_state": "longing"} # From "longingly" ) ] )]
prompt = textwrap.dedent("""\ Extract characters with contextual information. Use your knowledge of the source material to enrich attributes.""")examples = [ lx.data.ExampleData( text="Lady Juliet gazed longingly at the stars", extractions=[ lx.data.Extraction( extraction_class="character", extraction_text="Lady Juliet", attributes={ "emotional_state": "longing", "family": "Capulet", # From LLM knowledge "literary_role": "protagonist" # From LLM knowledge } ) ] )]