Skip to content

Commit f0e5668

Browse files
Merge branch 'master' into python-3.14
2 parents 26e3fa2 + 1d22735 commit f0e5668

41 files changed

Lines changed: 1323 additions & 1256 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

libs/cli/langchain_cli/integration_template/integration_template/chat_models.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ class Chat__ModuleName__(BaseChatModel):
3636
3737
# TODO: Populate with relevant params.
3838
Key init args — completion params:
39-
model: str
39+
model:
4040
Name of __ModuleName__ model to use.
41-
temperature: float
41+
temperature:
4242
Sampling temperature.
43-
max_tokens: int | None
43+
max_tokens:
4444
Max number of tokens to generate.
4545
4646
# TODO: Populate with relevant params.
4747
Key init args — client params:
48-
timeout: float | None
48+
timeout:
4949
Timeout for requests.
50-
max_retries: int
50+
max_retries:
5151
Max number of retries.
52-
api_key: str | None
52+
api_key:
5353
__ModuleName__ API key. If not passed in will be read from env var
5454
__MODULE_NAME___API_KEY.
5555

libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ class __ModuleName__VectorStore(VectorStore):
3737
3838
# TODO: Populate with relevant params.
3939
Key init args — indexing params:
40-
collection_name: str
40+
collection_name:
4141
Name of the collection.
42-
embedding_function: Embeddings
42+
embedding_function:
4343
Embedding function to use.
4444
4545
# TODO: Populate with relevant params.
4646
Key init args — client params:
47-
client: Client | None
47+
client:
4848
Client to use.
49-
connection_args: dict | None
49+
connection_args:
5050
Connection arguments.
5151
5252
# TODO: Replace with relevant init params.

libs/core/langchain_core/indexing/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,10 @@ def upsert(self, items: Sequence[Document], /, **kwargs: Any) -> UpsertResponse:
522522
523523
When an ID is specified and the content already exists in the vectorstore,
524524
the upsert method should update the content with the new data. If the content
525-
does not exist, the upsert method should add the item to the vectorstore.
525+
does not exist, the upsert method should add the item to the `VectorStore`.
526526
527527
Args:
528-
items: Sequence of documents to add to the vectorstore.
528+
items: Sequence of documents to add to the `VectorStore`.
529529
**kwargs: Additional keyword arguments.
530530
531531
Returns:
@@ -545,10 +545,10 @@ async def aupsert(
545545
546546
When an ID is specified and the item already exists in the vectorstore,
547547
the upsert method should update the item with the new data. If the item
548-
does not exist, the upsert method should add the item to the vectorstore.
548+
does not exist, the upsert method should add the item to the `VectorStore`.
549549
550550
Args:
551-
items: Sequence of documents to add to the vectorstore.
551+
items: Sequence of documents to add to the `VectorStore`.
552552
**kwargs: Additional keyword arguments.
553553
554554
Returns:

libs/core/langchain_core/language_models/base.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,14 @@ def generate_prompt(
200200
pure text generation models and `BaseMessage` objects for chat models).
201201
stop: Stop words to use when generating. Model output is cut off at the
202202
first occurrence of any of these substrings.
203-
callbacks: Callbacks to pass through. Used for executing additional
203+
callbacks: `Callbacks` to pass through. Used for executing additional
204204
functionality, such as logging or streaming, throughout generation.
205205
**kwargs: Arbitrary additional keyword arguments. These are usually passed
206206
to the model provider API call.
207207
208208
Returns:
209209
An `LLMResult`, which contains a list of candidate `Generation` objects for
210-
each input prompt and additional model provider-specific output.
210+
each input prompt and additional model provider-specific output.
211211
212212
"""
213213

@@ -237,14 +237,14 @@ async def agenerate_prompt(
237237
pure text generation models and `BaseMessage` objects for chat models).
238238
stop: Stop words to use when generating. Model output is cut off at the
239239
first occurrence of any of these substrings.
240-
callbacks: Callbacks to pass through. Used for executing additional
240+
callbacks: `Callbacks` to pass through. Used for executing additional
241241
functionality, such as logging or streaming, throughout generation.
242242
**kwargs: Arbitrary additional keyword arguments. These are usually passed
243243
to the model provider API call.
244244
245245
Returns:
246246
An `LLMResult`, which contains a list of candidate `Generation` objects for
247-
each input prompt and additional model provider-specific output.
247+
each input prompt and additional model provider-specific output.
248248
249249
"""
250250

@@ -269,8 +269,7 @@ def get_token_ids(self, text: str) -> list[int]:
269269
270270
Returns:
271271
A list of ids corresponding to the tokens in the text, in order they occur
272-
in the text.
273-
272+
in the text.
274273
"""
275274
if self.custom_get_token_ids is not None:
276275
return self.custom_get_token_ids(text)

libs/core/langchain_core/language_models/chat_models.py

Lines changed: 84 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -838,13 +838,13 @@ def generate(
838838
1. Take advantage of batched calls,
839839
2. Need more output from the model than just the top generated value,
840840
3. Are building chains that are agnostic to the underlying language model
841-
type (e.g., pure text completion models vs chat models).
841+
type (e.g., pure text completion models vs chat models).
842842
843843
Args:
844844
messages: List of list of messages.
845845
stop: Stop words to use when generating. Model output is cut off at the
846846
first occurrence of any of these substrings.
847-
callbacks: Callbacks to pass through. Used for executing additional
847+
callbacks: `Callbacks` to pass through. Used for executing additional
848848
functionality, such as logging or streaming, throughout generation.
849849
tags: The tags to apply.
850850
metadata: The metadata to apply.
@@ -854,8 +854,8 @@ def generate(
854854
to the model provider API call.
855855
856856
Returns:
857-
An LLMResult, which contains a list of candidate Generations for each input
858-
prompt and additional model provider-specific output.
857+
An `LLMResult`, which contains a list of candidate `Generations` for each
858+
input prompt and additional model provider-specific output.
859859
860860
"""
861861
ls_structured_output_format = kwargs.pop(
@@ -956,13 +956,13 @@ async def agenerate(
956956
1. Take advantage of batched calls,
957957
2. Need more output from the model than just the top generated value,
958958
3. Are building chains that are agnostic to the underlying language model
959-
type (e.g., pure text completion models vs chat models).
959+
type (e.g., pure text completion models vs chat models).
960960
961961
Args:
962962
messages: List of list of messages.
963963
stop: Stop words to use when generating. Model output is cut off at the
964964
first occurrence of any of these substrings.
965-
callbacks: Callbacks to pass through. Used for executing additional
965+
callbacks: `Callbacks` to pass through. Used for executing additional
966966
functionality, such as logging or streaming, throughout generation.
967967
tags: The tags to apply.
968968
metadata: The metadata to apply.
@@ -972,8 +972,8 @@ async def agenerate(
972972
to the model provider API call.
973973
974974
Returns:
975-
An LLMResult, which contains a list of candidate Generations for each input
976-
prompt and additional model provider-specific output.
975+
An `LLMResult`, which contains a list of candidate `Generations` for each
976+
input prompt and additional model provider-specific output.
977977
978978
"""
979979
ls_structured_output_format = kwargs.pop(
@@ -1510,115 +1510,121 @@ def with_structured_output(
15101510
If `schema` is a Pydantic class then the model output will be a
15111511
Pydantic instance of that class, and the model-generated fields will be
15121512
validated by the Pydantic class. Otherwise the model output will be a
1513-
dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool`
1514-
for more on how to properly specify types and descriptions of
1515-
schema fields when specifying a Pydantic or `TypedDict` class.
1513+
dict and will not be validated.
1514+
1515+
See `langchain_core.utils.function_calling.convert_to_openai_tool` for
1516+
more on how to properly specify types and descriptions of schema fields
1517+
when specifying a Pydantic or `TypedDict` class.
15161518
15171519
include_raw:
15181520
If `False` then only the parsed structured output is returned. If
15191521
an error occurs during model output parsing it will be raised. If `True`
15201522
then both the raw model response (a `BaseMessage`) and the parsed model
15211523
response will be returned. If an error occurs during output parsing it
1522-
will be caught and returned as well. The final output is always a dict
1523-
with keys `'raw'`, `'parsed'`, and `'parsing_error'`.
1524+
will be caught and returned as well.
1525+
1526+
The final output is always a `dict` with keys `'raw'`, `'parsed'`, and
1527+
`'parsing_error'`.
15241528
15251529
Raises:
15261530
ValueError: If there are any unsupported `kwargs`.
15271531
NotImplementedError: If the model does not implement
15281532
`with_structured_output()`.
15291533
15301534
Returns:
1531-
A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`.
1535+
A `Runnable` that takes same inputs as a
1536+
`langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is
1537+
`False` and `schema` is a Pydantic class, `Runnable` outputs an instance
1538+
of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is
1539+
`False` then `Runnable` outputs a `dict`.
15321540
1533-
If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs
1534-
an instance of `schema` (i.e., a Pydantic object).
1541+
If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys:
15351542
1536-
Otherwise, if `include_raw` is False then Runnable outputs a dict.
1543+
- `'raw'`: `BaseMessage`
1544+
- `'parsed'`: `None` if there was a parsing error, otherwise the type
1545+
depends on the `schema` as described above.
1546+
- `'parsing_error'`: `BaseException | None`
15371547
1538-
If `include_raw` is True, then Runnable outputs a dict with keys:
1548+
Example: Pydantic schema (`include_raw=False`):
15391549
1540-
- `'raw'`: BaseMessage
1541-
- `'parsed'`: None if there was a parsing error, otherwise the type
1542-
depends on the `schema` as described above.
1543-
- `'parsing_error'`: BaseException | None
1550+
```python
1551+
from pydantic import BaseModel
15441552
1545-
Example: Pydantic schema (include_raw=False):
1546-
```python
1547-
from pydantic import BaseModel
15481553
1554+
class AnswerWithJustification(BaseModel):
1555+
'''An answer to the user question along with justification for the answer.'''
15491556
1550-
class AnswerWithJustification(BaseModel):
1551-
'''An answer to the user question along with justification for the answer.'''
1557+
answer: str
1558+
justification: str
15521559
1553-
answer: str
1554-
justification: str
15551560
1561+
model = ChatModel(model="model-name", temperature=0)
1562+
structured_model = model.with_structured_output(AnswerWithJustification)
15561563
1557-
model = ChatModel(model="model-name", temperature=0)
1558-
structured_model = model.with_structured_output(AnswerWithJustification)
1564+
structured_model.invoke(
1565+
"What weighs more a pound of bricks or a pound of feathers"
1566+
)
15591567
1560-
structured_model.invoke(
1561-
"What weighs more a pound of bricks or a pound of feathers"
1562-
)
1568+
# -> AnswerWithJustification(
1569+
# answer='They weigh the same',
1570+
# justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
1571+
# )
1572+
```
15631573
1564-
# -> AnswerWithJustification(
1565-
# answer='They weigh the same',
1566-
# justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
1567-
# )
1568-
```
1574+
Example: Pydantic schema (`include_raw=True`):
15691575
1570-
Example: Pydantic schema (include_raw=True):
1571-
```python
1572-
from pydantic import BaseModel
1576+
```python
1577+
from pydantic import BaseModel
15731578
15741579
1575-
class AnswerWithJustification(BaseModel):
1576-
'''An answer to the user question along with justification for the answer.'''
1580+
class AnswerWithJustification(BaseModel):
1581+
'''An answer to the user question along with justification for the answer.'''
15771582
1578-
answer: str
1579-
justification: str
1583+
answer: str
1584+
justification: str
15801585
15811586
1582-
model = ChatModel(model="model-name", temperature=0)
1583-
structured_model = model.with_structured_output(
1584-
AnswerWithJustification, include_raw=True
1585-
)
1587+
model = ChatModel(model="model-name", temperature=0)
1588+
structured_model = model.with_structured_output(
1589+
AnswerWithJustification, include_raw=True
1590+
)
15861591
1587-
structured_model.invoke(
1588-
"What weighs more a pound of bricks or a pound of feathers"
1589-
)
1590-
# -> {
1591-
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
1592-
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
1593-
# 'parsing_error': None
1594-
# }
1595-
```
1592+
structured_model.invoke(
1593+
"What weighs more a pound of bricks or a pound of feathers"
1594+
)
1595+
# -> {
1596+
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
1597+
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
1598+
# 'parsing_error': None
1599+
# }
1600+
```
15961601
1597-
Example: Dict schema (include_raw=False):
1598-
```python
1599-
from pydantic import BaseModel
1600-
from langchain_core.utils.function_calling import convert_to_openai_tool
1602+
Example: `dict` schema (`include_raw=False`):
16011603
1604+
```python
1605+
from pydantic import BaseModel
1606+
from langchain_core.utils.function_calling import convert_to_openai_tool
16021607
1603-
class AnswerWithJustification(BaseModel):
1604-
'''An answer to the user question along with justification for the answer.'''
16051608
1606-
answer: str
1607-
justification: str
1609+
class AnswerWithJustification(BaseModel):
1610+
'''An answer to the user question along with justification for the answer.'''
16081611
1612+
answer: str
1613+
justification: str
16091614
1610-
dict_schema = convert_to_openai_tool(AnswerWithJustification)
1611-
model = ChatModel(model="model-name", temperature=0)
1612-
structured_model = model.with_structured_output(dict_schema)
16131615
1614-
structured_model.invoke(
1615-
"What weighs more a pound of bricks or a pound of feathers"
1616-
)
1617-
# -> {
1618-
# 'answer': 'They weigh the same',
1619-
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
1620-
# }
1621-
```
1616+
dict_schema = convert_to_openai_tool(AnswerWithJustification)
1617+
model = ChatModel(model="model-name", temperature=0)
1618+
structured_model = model.with_structured_output(dict_schema)
1619+
1620+
structured_model.invoke(
1621+
"What weighs more a pound of bricks or a pound of feathers"
1622+
)
1623+
# -> {
1624+
# 'answer': 'They weigh the same',
1625+
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
1626+
# }
1627+
```
16221628
16231629
!!! warning "Behavior changed in 0.2.26"
16241630
Added support for TypedDict class.

0 commit comments

Comments
 (0)