Tony20100/langchain_full huggingface dataset:
raise ValueError("x is 1")
    else:
         pass
runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass
assert (count == 2)
Parameters
retry_if_exception_type (Tuple[Type[BaseException], ...]) – A tuple of exception types to retry on
wait_exponential_jitter (bool) – Whether to add jitter to the wait time
between retries
stop_after_attempt (int) – The maximum number of attempts to make before giving up
Returns
A new Runnable that retries the original runnable on exceptions.
Return type
Runnable[Input, Output]
with_types(*, input_type: Optional[Type[Input]] = None, output_type: Optional[Type[Output]] = None) → Runnable[Input, Output]¶
Bind input and output types to a Runnable, returning a new Runnable.
Parameters
input_type (Optional[Type[Input]]) – 
output_type (Optional[Type[Output]]) – 
Return type
Runnable[Input, Output]
property InputType: Type[Input]¶
The type of input this runnable accepts specified as a type annotation.
property OutputType: Any¶
The type of output this runnable produces specified as a type annotation.
property config_specs: List[ConfigurableFieldSpec]¶
List configurable fields for this runnable.
property input_schema: Type[BaseModel]¶
The type of input this runnable accepts specified as a pydantic model.
property lc_attributes: Dict[str, Any]¶
List of attribute names that should be included in the serialized kwargs.
These attributes must be accepted by the constructor.
property lc_secrets: Dict[str, str]¶
property lc_secrets: Dict[str, str]¶
A map of constructor argument names to secret ids.
For example,{“openai_api_key”: “OPENAI_API_KEY”}
name: Optional[str] = None¶
The name of the runnable. Used for debugging and tracing.
property output_schema: Type[BaseModel]¶
The type of output this runnable produces specified as a pydantic model.
Examples using PromptTemplate¶
# Oracle Cloud Infrastructure Generative AI
AI21LLM
Aim
AirbyteLoader
Aleph Alpha
Alibaba Cloud PAI EAS
Amazon Document DB
AnthropicLLM
Anyscale
Apache AGE
Aphrodite Engine
Argilla
Azure ML
Banana
Baseten
Bittensor
Build a Local RAG Application
Build a Question/Answering system over SQL data
Build a Retrieval Augmented Generation (RAG) App
C Transformers
CTranslate2
CerebriumAI
ChatGLM
Clarifai
Cloudflare Workers AI
Cohere
Comet
Conceptual guide
Context
DSPy
Dall-E Image Generator
DeepInfra
Eden AI
ExLlamaV2
Exa Search
Fireworks
Flyte
ForefrontAI
GPT4All
Generate Synthetic Data
GigaChat
Google AI
Google Cloud Vertex AI
Google Cloud Vertex AI Reranker
Google Drive
GooseAI
Gradient
How to add fallbacks to a runnable
How to best prompt for Graph-RAG
How to better prompt when doing SQL question-answering
How to compose prompts together
How to configure runtime chain internals
How to parse JSON output
How to parse XML output
How to parse YAML output
How to partially format prompt templates
How to reorder retrieved results to mitigate the “lost in the middle” effect
How to reorder retrieved results to mitigate the “lost in the middle” effect
How to retry when a parsing error occurs
How to route between sub-chains
How to select examples by length
How to select examples by maximal marginal relevance (MMR)
How to select examples by n-gram overlap
How to select examples by similarity
How to track token usage for LLMs
How to use example selectors
How to use few shot examples
How to use output parsers to parse an LLM response into structured format
How to use the MultiQueryRetriever
Hugging Face Local Pipelines
Huggingface Endpoints
IBM watsonx.ai
IPEX-LLM
Intel Weight-Only Quantization
Javelin AI Gateway
Javelin AI Gateway Tutorial
Llama.cpp
MLX Local Pipelines
MLflow
MLflow AI Gateway
Manifest
Memgraph
Milvus Hybrid Search
Minimax
Modal
MongoDB Atlas
MosaicML
Motörhead
NLP Cloud
NVIDIA Riva: ASR and TTS
Nebula (Symbl.ai)
Neo4j
OctoAI
OllamaFunctions
OpaquePrompts
OpenAI
OpenLLM
OpenLM
OpenVINO
Petals
PipelineAI
Predibase
Prediction Guard
Ray Serve
RePhraseQuery
Rebuff
Reddit Search 
Replicate
Run LLMs locally
Runhouse
SAP HANA Cloud Vector Engine
SageMaker Tracking
SageMakerEndpoint
Shale Protocol
Solar
StochasticAI
Summarize Text
TextGen
Titan Takeoff
Tongyi Qwen
Volc Engine Maas
Weights & Biases
Writer
Xorbits Inference (Xinference)
YandexGPT
Zapier Natural Language Actions
vLLM
langchain_core.prompts.chat.ChatPromptTemplate¶
class langchain_core.prompts.chat.ChatPromptTemplate[source]¶
Bases: BaseChatPromptTemplate
Prompt template for chat models.
Use to create flexible templated prompts for chat models.
Examples
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful AI bot. Your name is {name}."),
    ("human", "Hello, how are you doing?"),
    ("ai", "I'm doing well, thanks!"),
    ("human", "{user_input}"),
])
prompt_value = template.invoke(
    {
        "name": "Bob",
        "user_input": "What is your name?"
    }
)
# Output:
# ChatPromptValue(
#    messages=[
#        SystemMessage(content='You are a helpful AI bot. Your name is Bob.'),
#        HumanMessage(content='Hello, how are you doing?'),
#        AIMessage(content="I'm doing well, thanks!"),
#        HumanMessage(content='What is your name?')
#    ]
#)
Messages Placeholder:
# In addition to Human/AI/Tool/Function messages,
# you can initialize the template with a MessagesPlaceholder
# either using the class directly or with the shorthand tuple syntax:
template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful AI bot."),
    # Means the template will receive an optional list of messages under
    # the "conversation" key
    ("placeholder", "{conversation}")
    # Equivalently:
    # MessagesPlaceholder(variable_name="conversation", optional=True)
])
prompt_value = template.invoke(
    {
        "conversation": [
            ("human", "Hi!"),
{
        "conversation": [
            ("human", "Hi!"),
            ("ai", "How can I assist you today?"),
            ("human", "Can you make me an ice cream sundae?"),
            ("ai", "No.")
        ]
    }
)
# Output:
# ChatPromptValue(
#    messages=[
#        SystemMessage(content='You are a helpful AI bot.'),
#        HumanMessage(content='Hi!'),
#        AIMessage(content='How can I assist you today?'),
#        HumanMessage(content='Can you make me an ice cream sundae?'),
#        AIMessage(content='No.'),
#    ]
#)
Single-variable template:
If your prompt has only a single input variable (i.e., 1 instance of “{variable_nams}”),
and you invoke the template with a non-dict object, the prompt template will
inject the provided argument into that variable location.
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful AI bot. Your name is Carl."),
    ("human", "{user_input}"),
])
prompt_value = template.invoke("Hello, there!")
# Equivalent to
# prompt_value = template.invoke({"user_input": "Hello, there!"})
# Output:
#  ChatPromptValue(
#     messages=[
#         SystemMessage(content='You are a helpful AI bot. Your name is Carl.'),
#         HumanMessage(content='Hello, there!'),
#     ]
# )
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
param input_types: Dict[str, Any] [Optional]¶
param input_types: Dict[str, Any] [Optional]¶
A dictionary of the types of the variables the prompt template expects.
If not provided, all variables are assumed to be strings.
param input_variables: List[str] [Required]¶
List of input variables in template messages. Used for validation.
param messages: List[MessageLike] [Required]¶
List of messages consisting of either message prompt templates or messages.
param metadata: Optional[Dict[str, Any]] = None¶
Metadata to be used for tracing.
param output_parser: Optional[BaseOutputParser] = None¶
How to parse the output of calling an LLM on this formatted prompt.
param partial_variables: Mapping[str, Any] [Optional]¶
A dictionary of the partial variables the prompt template carries.
Partial variables populate the template so that you don’t need to
pass them in every time you call the prompt.
param tags: Optional[List[str]] = None¶
Tags to be used for tracing.
param validate_template: bool = False¶
Whether or not to try validating the template.
async abatch(inputs: List[Input], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) → List[Output]¶
Default implementation runs ainvoke in parallel using asyncio.gather.
The default implementation of batch works well for IO bound runnables.
Subclasses should override this method if they can batch more efficiently;
e.g., if the underlying runnable uses an API which supports a batch mode.
Parameters
inputs (List[Input]) – 
config (Optional[Union[RunnableConfig, List[RunnableConfig]]]) – 
return_exceptions (bool) – 
kwargs (Optional[Any]) – 
Return type
List[Output]
kwargs (Optional[Any]) – 
Return type
List[Output]
async abatch_as_completed(inputs: Sequence[Input], config: Optional[Union[RunnableConfig, Sequence[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) → AsyncIterator[Tuple[int, Union[Output, Exception]]]¶
Run ainvoke in parallel on a list of inputs,
yielding results as they complete.
Parameters
inputs (Sequence[Input]) – 
config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) – 
return_exceptions (bool) – 
kwargs (Optional[Any]) – 
Return type
AsyncIterator[Tuple[int, Union[Output, Exception]]]
async aformat(**kwargs: Any) → str¶
Format the chat template into a string.
Parameters
**kwargs (Any) – keyword arguments to use for filling in template variables
in all the template messages in this chat template.
Returns
formatted string
Return type
str
async aformat_messages(**kwargs: Any) → List[BaseMessage][source]¶
Format the chat template into a list of finalized messages.
Parameters
**kwargs (Any) – keyword arguments to use for filling in template variables
in all the template messages in this chat template.
Returns
list of formatted messages
Return type
List[BaseMessage]
async aformat_prompt(**kwargs: Any) → PromptValue¶
Create Prompt Value.
Parameters
kwargs (Any) – 
Return type
PromptValue
async ainvoke(input: Dict, config: Optional[RunnableConfig] = None, **kwargs: Any) → PromptValue¶
Default implementation of ainvoke, calls invoke from a thread.
The default implementation allows usage of async code even if
the runnable did not implement a native async version of invoke.
the runnable did not implement a native async version of invoke.
Subclasses should override this method if they can run asynchronously.
Parameters
input (Dict) – 
config (Optional[RunnableConfig]) – 
kwargs (Any) – 
Return type
PromptValue
append(message: Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate, Tuple[Union[str, Type], Union[str, List[dict], List[object]]], str]) → None[source]¶
Append message to the end of the chat template.
Parameters
message (Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate, Tuple[Union[str, Type], Union[str, List[dict], List[object]]], str]) – representation of a message to append.
Return type
None
assign(**kwargs: Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any], Mapping[str, Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any]]]]) → RunnableSerializable[Any, Any]¶
Assigns new fields to the dict output of this runnable.
Returns a new runnable.
from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter
prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])
chain: Runnable = prompt | llm | {"str": StrOutputParser()}
chain_with_assign = chain.assign(hello=itemgetter("str") | llm)
print(chain_with_assign.input_schema.schema())
values (Any) – 
Return type
Model
copy(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, update: Optional[DictStrAny] = None, deep: bool = False) → Model¶
Duplicate a model, optionally choose which fields to include, exclude and change.
Parameters
include (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to include in new model
exclude (Optional[Union[AbstractSetIntStr, MappingIntStrAny]]) – fields to exclude from new model, as with values this takes precedence over include
update (Optional[DictStrAny]) – values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
deep (bool) – set to True to make a deep copy of the model
self (Model) – 
Returns
new model instance
Return type
Model
dict(**kwargs: Any) → Dict¶
Return dictionary representation of prompt.
Parameters
kwargs (Any) – 
Return type
Dict
extend(messages: Sequence[Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate, Tuple[Union[str, Type], Union[str, List[dict], List[object]]], str]]) → None[source]¶
Extend the chat template with a sequence of messages.
Parameters
messages (Sequence[Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate, Tuple[Union[str, Type], Union[str, List[dict], List[object]]], str]]) – 
Return type
None
format(**kwargs: Any) → str¶
Format the chat template into a string.
Parameters
**kwargs (Any) – keyword arguments to use for filling in template variables
Parameters
**kwargs (Any) – keyword arguments to use for filling in template variables
in all the template messages in this chat template.
Returns
formatted string
Return type
str
format_messages(**kwargs: Any) → List[BaseMessage][source]¶
Format the chat template into a list of finalized messages.
Parameters
**kwargs (Any) – keyword arguments to use for filling in template variables
in all the template messages in this chat template.
Returns
list of formatted messages
Return type
List[BaseMessage]
format_prompt(**kwargs: Any) → PromptValue¶
Format prompt. Should return a PromptValue.
:param **kwargs: Keyword arguments to use for formatting.
Returns
PromptValue.
Parameters
kwargs (Any) – 
Return type
PromptValue
classmethod from_messages(messages: Sequence[Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate, Tuple[Union[str, Type], Union[str, List[dict], List[object]]], str]], template_format: Literal['f-string', 'mustache'] = 'f-string') → ChatPromptTemplate[source]¶
Create a chat prompt template from a variety of message formats.
Examples
Instantiation from a list of message templates:
template = ChatPromptTemplate.from_messages([
    ("human", "Hello, how are you?"),
    ("ai", "I'm doing well, thanks!"),
    ("human", "That's good to hear."),
])
Instantiation from mixed message formats:
template = ChatPromptTemplate.from_messages([
    SystemMessage(content="hello"),
    ("human", "Hello, how are you?"),
])
Parameters
messages (Sequence[Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate, Tuple[Union[str, Type], Union[str, List[dict], List[object]]], str]]) – sequence of message representations.
A message can be represented using the following formats:
(1) BaseMessagePromptTemplate, (2) BaseMessage, (3) 2-tuple of
(message type, template); e.g., (“human”, “{user_input}”),
(4) 2-tuple of (message class, template), (4) a string which is
shorthand for (“human”, template); e.g., “{user_input}”
template_format (Literal['f-string', 'mustache']) – 
Returns
a chat prompt template
Return type
ChatPromptTemplate
classmethod from_orm(obj: Any) → Model¶
Parameters
obj (Any) – 
Return type
Model
classmethod from_role_strings(string_messages: List[Tuple[str, str]]) → ChatPromptTemplate[source]¶
[Deprecated] Create a chat prompt template from a list of (role, template) tuples.
Parameters
string_messages (List[Tuple[str, str]]) – list of (role, template) tuples.
Returns
a chat prompt template
Return type
ChatPromptTemplate
Notes
Deprecated since version langchain-core==0.0.1: Use from_messages classmethod instead.
classmethod from_strings(string_messages: List[Tuple[Type[BaseMessagePromptTemplate], str]]) → ChatPromptTemplate[source]¶
[Deprecated] Create a chat prompt template from a list of (role class, template) tuples.
Parameters
string_messages (List[Tuple[Type[BaseMessagePromptTemplate], str]]) – list of (role class, template) tuples.
Returns
a chat prompt template
Return type
ChatPromptTemplate
Notes
Deprecated since version langchain-core==0.0.1: Use from_messages classmethod instead.
classmethod from_template(template: str, **kwargs: Any) → ChatPromptTemplate[source]¶
Create a chat prompt template from a template string.
Create a chat prompt template from a template string.
Creates a chat template consisting of a single message assumed to be from
the human.
Parameters
template (str) – template string
**kwargs (Any) – keyword arguments to pass to the constructor.
Returns
A new instance of this class.
Return type
ChatPromptTemplate
get_graph(config: Optional[RunnableConfig] = None) → Graph¶
Return a graph representation of this runnable.
Parameters
config (Optional[RunnableConfig]) – 
Return type
Graph
get_input_schema(config: Optional[RunnableConfig] = None) → Type[BaseModel]¶
Get a pydantic model that can be used to validate input to the runnable.
Runnables that leverage the configurable_fields and configurable_alternatives
methods will have a dynamic input schema that depends on which
configuration the runnable is invoked with.
This method allows to get an input schema for a specific configuration.
Parameters
config (Optional[RunnableConfig]) – A config to use when generating the schema.
Returns
A pydantic model that can be used to validate input.
Return type
Type[BaseModel]
classmethod get_lc_namespace() → List[str][source]¶
Get the namespace of the langchain object.
Return type
List[str]
get_name(suffix: Optional[str] = None, *, name: Optional[str] = None) → str¶
Get the name of the runnable.
Parameters
suffix (Optional[str]) – 
name (Optional[str]) – 
Return type
str
get_output_schema(config: Optional[RunnableConfig] = None) → Type[BaseModel]¶
Get a pydantic model that can be used to validate output to the runnable.
Runnables that leverage the configurable_fields and configurable_alternatives
methods will have a dynamic output schema that depends on which
configuration the runnable is invoked with.
methods will have a dynamic output schema that depends on which
configuration the runnable is invoked with.
This method allows to get an output schema for a specific configuration.
Parameters
config (Optional[RunnableConfig]) – A config to use when generating the schema.
Returns
A pydantic model that can be used to validate output.
Return type
Type[BaseModel]
get_prompts(config: Optional[RunnableConfig] = None) → List[BasePromptTemplate]¶
Parameters
config (Optional[RunnableConfig]) – 
Return type
List[BasePromptTemplate]
invoke(input: Dict, config: Optional[RunnableConfig] = None) → PromptValue¶
Transform a single input into an output. Override to implement.
Parameters
input (Dict) – The input to the runnable.
config (Optional[RunnableConfig]) – A config to use when invoking the runnable.
The config supports standard keys like ‘tags’, ‘metadata’ for tracing
purposes, ‘max_concurrency’ for controlling how much work to do
in parallel, and other keys. Please refer to the RunnableConfig
for more details.
Returns
The output of the runnable.
Return type
PromptValue
classmethod is_lc_serializable() → bool¶
Return whether this class is serializable.
Return type
bool
json(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, by_alias: bool = False, skip_defaults: Optional[bool] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, encoder: Optional[Callable[[Any], Any]] = None, models_as_dict: bool = True, **dumps_kwargs: Any) → unicode¶
encoding (unicode) – 
proto (Protocol) – 
allow_pickle (bool) – 
Return type
Model
classmethod parse_obj(obj: Any) → Model¶
Parameters
obj (Any) – 
Return type
Model
classmethod parse_raw(b: Union[str, bytes], *, content_type: unicode = None, encoding: unicode = 'utf8', proto: Protocol = None, allow_pickle: bool = False) → Model¶
Parameters
b (Union[str, bytes]) – 
content_type (unicode) – 
encoding (unicode) – 
proto (Protocol) – 
allow_pickle (bool) – 
Return type
Model
partial(**kwargs: Any) → ChatPromptTemplate[source]¶
Get a new ChatPromptTemplate with some input variables already filled in.
Parameters
**kwargs (Any) – keyword arguments to use for filling in template variables. Ought
to be a subset of the input variables.
Returns
A new ChatPromptTemplate.
Return type
ChatPromptTemplate
Example
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are an AI assistant named {name}."),
        ("human", "Hi I'm {user}"),
        ("ai", "Hi there, {user}, I'm {name}."),
        ("human", "{input}"),
    ]
)
template2 = template.partial(user="Lucy", name="R2D2")
template2.format_messages(input="hello")
pick(keys: Union[str, List[str]]) → RunnableSerializable[Any, Any]¶
Pick keys from the dict output of this runnable.
Pick single key:import json
from langchain_core.runnables import RunnableLambda, RunnableMap
as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
Compose this Runnable with Runnable-like objects to make a RunnableSequence.
Equivalent to RunnableSequence(self, *others) or self | others[0] | …
Example
from langchain_core.runnables import RunnableLambda
def add_one(x: int) -> int:
    return x + 1
def mul_two(x: int) -> int:
    return x * 2
runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4
sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]
Parameters
others (Union[Runnable[Any, Other], Callable[[Any], Other]]) – 
name (Optional[str]) – 
Return type
RunnableSerializable[Input, Other]
pretty_print() → None¶
Return type
None
pretty_repr(html: bool = False) → str[source]¶
Human-readable representation.
Parameters
html (bool) – 
Return type
str
save(file_path: Union[Path, str]) → None[source]¶
Save prompt to file.
Parameters
file_path (Union[Path, str]) – path to file.
Return type
None
classmethod schema(by_alias: bool = True, ref_template: unicode = '#/definitions/{model}') → DictStrAny¶
Parameters
by_alias (bool) – 
ref_template (unicode) – 
Return type
DictStrAny