Instructions for using LangChain with Python
# LangChain Python Instructions These instructions guide GitHub Copilot in generating code and documentation for LangChain applications in Python. Focus on LangChain-specific patterns, APIs, and best practices. ## Runnable Interface (LangChain-specific) LangChain's `Runnable` interface is the foundation for composing and executing chains, chat models, output parsers, retrievers, and LangGraph graphs. It provides a unified API for invoking, batching, streaming, inspecting, and composing components. **Key LangChain-specific features:** - All major LangChain components (chat models, output parsers, retrievers, graphs) implement the Runnable interface. - Supports synchronous (`invoke`, `batch`, `stream`) and asynchronous (`ainvoke`, `abatch`, `astream`) execution. - Batching (`batch`, `batch_as_completed`) is optimized for parallel API calls; set `max_concurrency` in `RunnableConfig` to control parallelism. - Streaming APIs (`stream`, `astream`, `astream_events`) yield outputs as they are produced, critical for responsive LLM apps. - Input/output types are component-specific (e.g., chat models accept messages, retrievers accept strings, output parsers accept model outputs). - Inspect schemas with `get_input_schema`, `get_output_schema`, and their JSONSchema variants for validation and OpenAPI generation. - Use `with_types` to override inferred input/output types for complex LCEL chains. - Compose Runnables declaratively with LCEL: `chain = prompt | chat_model | output_parser`. - Propagate `RunnableConfig` (tags, metadata, callbacks, concurrency) automatically in Python 3.11+; manually in async code for Python 3.9/3.10. - Create custom runnables with `RunnableLambda` (simple transforms) or `RunnableGenerator` (streaming transforms); avoid subclassing directly. - Configure runtime attributes and alternatives with `configurable_fields` and `configurable_alternatives` for dynamic chains and LangServe deployments. **LangChain best practices:** - Use batching for parallel API calls to LLMs or retrievers; set `max_concurrency` to avoid rate limits.
Sign in to view the full prompt.
Sign In