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¶
Return a 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]¶
constructor.
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 ChatPromptTemplate¶
AWS DynamoDB
Activeloop Deep Memory
Apache Cassandra
AskNews
Astra DB
Build a Chatbot
Build a Query Analysis System
Build a Retrieval Augmented Generation (RAG) App
Build a Simple LLM Application
Build an Extraction Chain
ChatAI21
ChatAnthropic
ChatOllama
ChatOpenAI
ChatPerplexity
ChatUpstage
ChatVertexAI
Classify Text into Labels
Cohere
Conceptual guide
Context
Conversational RAG
Eden AI
Facebook Messenger
Fiddler
Figma
Fleet AI Context
Google AlloyDB for PostgreSQL
Google El Carro Oracle
Google SQL for MySQL
Google SQL for PostgreSQL
Google SQL for SQL Server
Groq
How deal with high cardinality categoricals when doing query analysis
How to add a semantic layer over graph database
How to add ad-hoc tool calling capability to LLMs and Chat Models
How to add chat history
How to add default invocation args to a Runnable
How to add examples to the prompt for query analysis
How to add fallbacks to a runnable
How to add memory to chatbots
How to add message history
How to add retrieval to chatbots
How to add tools to chatbots
How to add values to a chain’s state
How to attach callbacks to a runnable
How to chain runnables
How to create a custom LLM class
How to chain runnables
How to create a custom LLM class
How to create a dynamic (self-constructing) chain
How to create custom callback handlers
How to deal with large databases when doing SQL question-answering
How to debug your LLM apps
How to do per-user retrieval
How to do query validation as part of SQL question-answering
How to do question answering over CSVs
How to do tool/function calling
How to get a RAG application to add citations
How to get your RAG application to return sources
How to handle cases where no queries are generated
How to handle long text when doing extraction
How to handle multiple queries when doing query analysis
How to handle multiple retrievers when doing query analysis
How to handle tool errors
How to inspect runnables
How to invoke runnables in parallel
How to map values to a graph database
How to migrate from legacy LangChain agents to LangGraph
How to pass callbacks in at runtime
How to pass through arguments from one step to the next
How to propagate callbacks  constructor
How to retrieve using multiple vectors per document
How to return structured data from a model
How to run custom functions
How to stream results from your RAG application
How to stream runnables
How to track token usage in ChatModels
How to use a model to call tools
How to use few shot examples in chat models
How to use multimodal prompts
How to use prompting alone (no tool calling) to do extraction
How to use reference examples when doing extraction
Hybrid Search
Jaguar Vector Database
JinaChat
Kinetica SqlAssist LLM Demo
LangChain Expression Language Cheatsheet
LangSmith LLM Runs
Llama2Chat
Maritalk
MistralAI
MongoDB
NVIDIA NIMs
NVIDIA NIMs
MongoDB
NVIDIA NIMs
NVIDIA NIMs 
OpenAI metadata tagger
RAGatouille
Redis
SQL (SQLAlchemy)
SQL Database
SQLite
Streamlit
Tavily Search API
TiDB
UpTrain
Vectara
Vector stores and retrievers
Weaviate
Yellowbrick
You.com
Yuan2.0
ZepCloudChatMessageHistory
iMessage
vLLM Chat
🦜️🏓 LangServe
langchain_text_splitters.html.HTMLSectionSplitter¶
class langchain_text_splitters.html.HTMLSectionSplitter(headers_to_split_on: List[Tuple[str, str]], xslt_path: Optional[str] = None, **kwargs: Any)[source]¶
Splitting HTML files based on specified tag and font sizes.
Requires lxml package.
Create a new HTMLSectionSplitter.
Parameters
headers_to_split_on (List[Tuple[str, str]]) – list of tuples of headers we want to track mapped to
(arbitrary) keys for metadata. Allowed header values: h1, h2, h3, h4,
h5, h6 e.g. [(“h1”, “Header 1”), (“h2”, “Header 2”].
xslt_path (Optional[str]) – path to xslt file for document transformation.
passed. (Uses a default if not) – 
layouts. (Needed for html contents that using different format and) – 
kwargs (Any) – 
Methods
__init__(headers_to_split_on[, xslt_path])
Create a new HTMLSectionSplitter.
convert_possible_tags_to_header(html_content)
create_documents(texts[, metadatas])
Create documents from a list of texts.
split_documents(documents)
Split documents.
split_html_by_headers(html_doc)
split_text(text)
Split HTML text string
split_text_from_file(file)
Split HTML file
__init__(headers_to_split_on: List[Tuple[str, str]], xslt_path: Optional[str] = None, **kwargs: Any) → None[source]¶
Create a new HTMLSectionSplitter.
Parameters
headers_to_split_on (List[Tuple[str, str]]) – list of tuples of headers we want to track mapped to
(arbitrary) keys for metadata. Allowed header values: h1, h2, h3, h4,
h5, h6 e.g. [(“h1”, “Header 1”), (“h2”, “Header 2”].
xslt_path (Optional[str]) – path to xslt file for document transformation.
passed. (Uses a default if not) – 
layouts. (Needed for html contents that using different format and) – 
kwargs (Any) – 
Return type
None
convert_possible_tags_to_header(html_content: str) → str[source]¶
Parameters
html_content (str) – 
Return type
str
create_documents(texts: List[str], metadatas: Optional[List[dict]] = None) → List[Document][source]¶
Create documents from a list of texts.
Parameters
texts (List[str]) – 
metadatas (Optional[List[dict]]) – 
Return type
List[Document]
split_documents(documents: Iterable[Document]) → List[Document][source]¶
Split documents.
Parameters
documents (Iterable[Document]) – 
Return type
List[Document]
split_html_by_headers(html_doc: str) → Dict[str, Dict[str, Optional[str]]][source]¶
Parameters
html_doc (str) – 
Return type
Dict[str, Dict[str, Optional[str]]]
split_text(text: str) → List[Document][source]¶
Split HTML text string
Parameters
text (str) – HTML text
Return type
List[Document]
split_text_from_file(file: Any) → List[Document][source]¶
Split HTML file
Parameters
file (Any) – HTML file
Return type
List[Document]
Examples using HTMLSectionSplitter¶
How to split by HTML sections
langchain_text_splitters.html.HTMLHeaderTextSplitter¶
class langchain_text_splitters.html.HTMLHeaderTextSplitter(headers_to_split_on: List[Tuple[str, str]], return_each_element: bool = False)[source]¶
Splitting HTML files based on specified headers.
Requires lxml package.
Create a new HTMLHeaderTextSplitter.
Parameters
headers_to_split_on (List[Tuple[str, str]]) – list of tuples of headers we want to track mapped to
(arbitrary) keys for metadata. Allowed header values: h1, h2, h3, h4,
h5, h6 e.g. [(“h1”, “Header 1”), (“h2”, “Header 2)].
return_each_element (bool) – Return each element w/ associated headers.
Methods
__init__(headers_to_split_on[, ...])
Create a new HTMLHeaderTextSplitter.
aggregate_elements_to_chunks(elements)
Combine elements with common metadata into chunks
split_text(text)
Split HTML text string
split_text_from_file(file)
Split HTML file
split_text_from_url(url)
Split HTML from web URL
__init__(headers_to_split_on: List[Tuple[str, str]], return_each_element: bool = False)[source]¶
Create a new HTMLHeaderTextSplitter.
Parameters
headers_to_split_on (List[Tuple[str, str]]) – list of tuples of headers we want to track mapped to
(arbitrary) keys for metadata. Allowed header values: h1, h2, h3, h4,
h5, h6 e.g. [(“h1”, “Header 1”), (“h2”, “Header 2)].
return_each_element (bool) – Return each element w/ associated headers.
aggregate_elements_to_chunks(elements: List[ElementType]) → List[Document][source]¶
Combine elements with common metadata into chunks
Parameters
Combine elements with common metadata into chunks
Parameters
elements (List[ElementType]) – HTML element content with associated identifying info and metadata
Return type
List[Document]
split_text(text: str) → List[Document][source]¶
Split HTML text string
Parameters
text (str) – HTML text
Return type
List[Document]
split_text_from_file(file: Any) → List[Document][source]¶
Split HTML file
Parameters
file (Any) – HTML file
Return type
List[Document]
split_text_from_url(url: str) → List[Document][source]¶
Split HTML from web URL
Parameters
url (str) – web URL
Return type
List[Document]
Examples using HTMLHeaderTextSplitter¶
How to split by HTML header
langchain_google_vertexai.functions_utils.PydanticFunctionsOutputParser¶
class langchain_google_vertexai.functions_utils.PydanticFunctionsOutputParser[source]¶
Bases: BaseOutputParser
Parse an output as a pydantic object.
This parser is used to parse the output of a ChatModel that uses
Google Vertex function format to invoke functions.
The parser extracts the function call invocation and matches
them to the pydantic schema provided.
An exception will be raised if the function call does not match
the provided schema.
Example
… code-block:: python
message = AIMessage(content=”This is a test message”,
additional_kwargs={
“function_call”: {“name”: “cookie”,
“arguments”: json.dumps({“name”: “value”, “age”: 10}),
}
},
)
chat_generation = ChatGeneration(message=message)
class Cookie(BaseModel):name: str
age: int
class Dog(BaseModel):species: str
# Full output
parser = PydanticOutputFunctionsParser(
pydantic_schema={“cookie”: Cookie, “dog”: Dog}
)
result = parser.parse_result([chat_generation])
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 pydantic_schema: Union[Type[BaseModel], Dict[str, Type[BaseModel]]] [Required]¶
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;
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]
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 ainvoke(input: Union[str, BaseMessage], config: Optional[RunnableConfig] = None, **kwargs: Optional[Any]) → T¶
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.
Subclasses should override this method if they can run asynchronously.
Parameters
input (Union[str, BaseMessage]) – 
config (Optional[RunnableConfig]) – 
kwargs (Optional[Any]) – 
Return type
T
async aparse(text: str) → T¶
Parse a single string model output into some structure.
Parameters
text (str) – String output of a language model.
Returns
Structured output.
Return type
T
Returns
Structured output.
Return type
T
async aparse_result(result: List[Generation], *, partial: bool = False) → T¶
Parse a list of candidate model Generations into a specific format.
The return value is parsed from only the first Generation in the result, whichis assumed to be the highest-likelihood Generation.
Parameters
result (List[Generation]) – A list of Generations to be parsed. The Generations are assumed
to be different candidate outputs for a single model input.
partial (bool) – 
Returns
Structured output.
Return type
T
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())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.schema()) #
skip_defaults (Optional[bool]) – 
exclude_unset (bool) – 
exclude_defaults (bool) – 
exclude_none (bool) – 
encoder (Optional[Callable[[Any], Any]]) – 
models_as_dict (bool) – 
dumps_kwargs (Any) – 
Return type
unicode
classmethod lc_id() → List[str]¶
A unique identifier for this class for serialization purposes.
The unique identifier is a list of strings that describes the path
to the object.
Return type
List[str]
map() → Runnable[List[Input], List[Output]]¶
Return a new Runnable that maps a list of inputs to a list of outputs,
by calling invoke() with each input.
Example
from langchain_core.runnables import RunnableLambda
def _lambda(x: int) -> int:
    return x + 1
runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3])) # [2, 3, 4]
Return type
Runnable[List[Input], List[Output]]
parse(text: str) → BaseModel[source]¶
Parse a single string model output into some structure.
Parameters
text (str) – String output of a language model.
Returns
Structured output.
Return type
BaseModel
classmethod parse_file(path: Union[str, Path], *, content_type: unicode = None, encoding: unicode = 'utf8', proto: Protocol = None, allow_pickle: bool = False) → Model¶
Parameters
path (Union[str, Path]) – 
content_type (unicode) – 
encoding (unicode) – 
proto (Protocol) – 
allow_pickle (bool) – 
Return type
Model
classmethod parse_obj(obj: Any) → Model¶
Parameters
obj (Any) – 
Return type
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
parse_result(result: List[Generation], *, partial: bool = False) → BaseModel[source]¶
Parse a list of candidate model Generations into a specific format.
The return value is parsed from only the first Generation in the result, whichis assumed to be the highest-likelihood Generation.
Parameters
result (List[Generation]) – A list of Generations to be parsed. The Generations are assumed
to be different candidate outputs for a single model input.
partial (bool) – 
Returns
Structured output.
Return type
BaseModel
parse_with_prompt(completion: str, prompt: PromptValue) → Any¶
Parse the output of an LLM call with the input prompt for context.
The prompt is largely provided in the event the OutputParser wants
to retry or fix the output in some way, and needs information from
the prompt to do so.
Parameters
completion (str) – String output of a language model.
prompt (PromptValue) – Input PromptValue.
Returns
Structured output
Return type
Any
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)
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: Any¶
The type of input this runnable accepts specified as a type annotation.
property OutputType: Type[T]¶
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¶
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]¶
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.
langchain_google_community.gcs_directory.GCSDirectoryLoader¶
class langchain_google_community.gcs_directory.GCSDirectoryLoader(project_name: str, bucket: str, prefix: str = '', loader_func: Optional[Callable[[str], BaseLoader]] = None)[source]¶
Load from GCS directory.
Initialize with bucket and key name.
Parameters
project_name (str) – The ID of the project for the GCS bucket.
bucket (str) – The name of the GCS bucket.
prefix (str) – The prefix of the GCS bucket.
loader_func (Optional[Callable[[str], BaseLoader]]) – A loader function that instantiates a loader based on a
file_path argument. If nothing is provided, the  GCSFileLoader
would use its default loader.
Methods
__init__(project_name, bucket[, prefix, ...])
Initialize with bucket and key name.
alazy_load()
A lazy loader for Documents.
aload()
Load data into Document objects.
lazy_load()
A lazy loader for Documents.
load()
Load documents.
load_and_split([text_splitter])
Load Documents and split into chunks.
__init__(project_name: str, bucket: str, prefix: str = '', loader_func: Optional[Callable[[str], BaseLoader]] = None)[source]¶
Initialize with bucket and key name.
Parameters
project_name (str) – The ID of the project for the GCS bucket.
bucket (str) – The name of the GCS bucket.
prefix (str) – The prefix of the GCS bucket.
loader_func (Optional[Callable[[str], BaseLoader]]) – A loader function that instantiates a loader based on a
file_path argument. If nothing is provided, the  GCSFileLoader
would use its default loader.
would use its default loader.
async alazy_load() → AsyncIterator[Document]¶
A lazy loader for Documents.
Return type
AsyncIterator[Document]
async aload() → List[Document]¶
Load data into Document objects.
Return type
List[Document]
lazy_load() → Iterator[Document]¶
A lazy loader for Documents.
Return type
Iterator[Document]
load() → List[Document][source]¶
Load documents.
Return type
List[Document]
load_and_split(text_splitter: Optional[TextSplitter] = None) → List[Document]¶
Load Documents and split into chunks. Chunks are returned as Documents.
Do not override this method. It should be considered to be deprecated!
Parameters
text_splitter (Optional[TextSplitter]) – TextSplitter instance to use for splitting documents.
Defaults to RecursiveCharacterTextSplitter.
Returns
List of Documents.
Return type
List[Document]
langchain_experimental.tot.memory.ToTDFSMemory¶
class langchain_experimental.tot.memory.ToTDFSMemory(stack: Optional[List[Thought]] = None)[source]¶
Memory for the Tree of Thought (ToT) chain.
It is implemented as a stack of
thoughts. This allows for a depth first search (DFS) of the ToT.
Attributes
level
Return the current level of the stack.
Methods
__init__([stack])
current_path()
Return the thoughts path.
pop([n])
Pop the top n elements of the stack and return the last one.
store(node)
Add a node on the top of the stack.
top()
Get the top of the stack without popping it.
top_parent()
Get the parent of the top of the stack without popping it.
Parameters
stack (Optional[List[Thought]]) – 
__init__(stack: Optional[List[Thought]] = None)[source]¶
Parameters
stack (Optional[List[Thought]]) – 
current_path() → List[Thought][source]¶
Return the thoughts path.
Return type
List[Thought]
pop(n: int = 1) → Optional[Thought][source]¶
Pop the top n elements of the stack and return the last one.
Parameters
n (int) – 
Return type
Optional[Thought]
store(node: Thought) → None[source]¶
Add a node on the top of the stack.
Parameters
node (Thought) – 
Return type
None
top() → Optional[Thought][source]¶
Get the top of the stack without popping it.
Return type
Optional[Thought]
top_parent() → Optional[Thought][source]¶
Get the parent of the top of the stack without popping it.
Return type
Optional[Thought]