Skip to content

Commit e023201

Browse files
authored
style: some cleanup (#33857)
1 parent d40e340 commit e023201

File tree

79 files changed

+662
-531
lines changed

Some content is hidden

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

79 files changed

+662
-531
lines changed

libs/core/langchain_core/example_selectors/length_based.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class LengthBasedExampleSelector(BaseExampleSelector, BaseModel):
2929
max_length: int = 2048
3030
"""Max length for the prompt, beyond which examples are cut."""
3131

32-
example_text_lengths: list[int] = Field(default_factory=list) # :meta private:
32+
example_text_lengths: list[int] = Field(default_factory=list)
3333
"""Length of each example."""
3434

3535
def add_example(self, example: dict[str, str]) -> None:

libs/core/langchain_core/language_models/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,19 @@ class BaseLanguageModel(
131131
132132
Caching is not currently supported for streaming methods of models.
133133
"""
134+
134135
verbose: bool = Field(default_factory=_get_verbosity, exclude=True, repr=False)
135136
"""Whether to print out response text."""
137+
136138
callbacks: Callbacks = Field(default=None, exclude=True)
137139
"""Callbacks to add to the run trace."""
140+
138141
tags: list[str] | None = Field(default=None, exclude=True)
139142
"""Tags to add to the run trace."""
143+
140144
metadata: dict[str, Any] | None = Field(default=None, exclude=True)
141145
"""Metadata to add to the run trace."""
146+
142147
custom_get_token_ids: Callable[[str], list[int]] | None = Field(
143148
default=None, exclude=True
144149
)

libs/core/langchain_core/language_models/chat_models.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,10 +1515,10 @@ def with_structured_output(
15151515
Args:
15161516
schema: The output schema. Can be passed in as:
15171517
1518-
- an OpenAI function/tool schema,
1519-
- a JSON Schema,
1520-
- a `TypedDict` class,
1521-
- or a Pydantic class.
1518+
- An OpenAI function/tool schema,
1519+
- A JSON Schema,
1520+
- A `TypedDict` class,
1521+
- Or a Pydantic class.
15221522
15231523
If `schema` is a Pydantic class then the model output will be a
15241524
Pydantic instance of that class, and the model-generated fields will be
@@ -1530,11 +1530,15 @@ def with_structured_output(
15301530
when specifying a Pydantic or `TypedDict` class.
15311531
15321532
include_raw:
1533-
If `False` then only the parsed structured output is returned. If
1534-
an error occurs during model output parsing it will be raised. If `True`
1535-
then both the raw model response (a `BaseMessage`) and the parsed model
1536-
response will be returned. If an error occurs during output parsing it
1537-
will be caught and returned as well.
1533+
If `False` then only the parsed structured output is returned.
1534+
1535+
If an error occurs during model output parsing it will be raised.
1536+
1537+
If `True` then both the raw model response (a `BaseMessage`) and the
1538+
parsed model response will be returned.
1539+
1540+
If an error occurs during output parsing it will be caught and returned
1541+
as well.
15381542
15391543
The final output is always a `dict` with keys `'raw'`, `'parsed'`, and
15401544
`'parsing_error'`.
@@ -1640,7 +1644,7 @@ class AnswerWithJustification(BaseModel):
16401644
```
16411645
16421646
!!! warning "Behavior changed in `langchain-core` 0.2.26"
1643-
Added support for TypedDict class.
1647+
Added support for `TypedDict` class.
16441648
16451649
""" # noqa: E501
16461650
_ = kwargs.pop("method", None)

libs/core/langchain_core/runnables/base.py

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -829,8 +829,9 @@ def invoke(
829829
830830
The config supports standard keys like `'tags'`, `'metadata'` for
831831
tracing purposes, `'max_concurrency'` for controlling how much work to
832-
do in parallel, and other keys. Please refer to the `RunnableConfig`
833-
for more details.
832+
do in parallel, and other keys.
833+
834+
Please refer to `RunnableConfig` for more details.
834835
835836
Returns:
836837
The output of the `Runnable`.
@@ -850,8 +851,9 @@ async def ainvoke(
850851
851852
The config supports standard keys like `'tags'`, `'metadata'` for
852853
tracing purposes, `'max_concurrency'` for controlling how much work to
853-
do in parallel, and other keys. Please refer to the `RunnableConfig`
854-
for more details.
854+
do in parallel, and other keys.
855+
856+
Please refer to `RunnableConfig` for more details.
855857
856858
Returns:
857859
The output of the `Runnable`.
@@ -878,8 +880,9 @@ def batch(
878880
config: A config to use when invoking the `Runnable`. The config supports
879881
standard keys like `'tags'`, `'metadata'` for
880882
tracing purposes, `'max_concurrency'` for controlling how much work
881-
to do in parallel, and other keys. Please refer to the
882-
`RunnableConfig` for more details.
883+
to do in parallel, and other keys.
884+
885+
Please refer to `RunnableConfig` for more details.
883886
return_exceptions: Whether to return exceptions instead of raising them.
884887
**kwargs: Additional keyword arguments to pass to the `Runnable`.
885888
@@ -945,8 +948,9 @@ def batch_as_completed(
945948
946949
The config supports standard keys like `'tags'`, `'metadata'` for
947950
tracing purposes, `'max_concurrency'` for controlling how much work to
948-
do in parallel, and other keys. Please refer to the `RunnableConfig`
949-
for more details.
951+
do in parallel, and other keys.
952+
953+
Please refer to `RunnableConfig` for more details.
950954
return_exceptions: Whether to return exceptions instead of raising them.
951955
**kwargs: Additional keyword arguments to pass to the `Runnable`.
952956
@@ -1012,8 +1016,9 @@ async def abatch(
10121016
10131017
The config supports standard keys like `'tags'`, `'metadata'` for
10141018
tracing purposes, `'max_concurrency'` for controlling how much work to
1015-
do in parallel, and other keys. Please refer to the `RunnableConfig`
1016-
for more details.
1019+
do in parallel, and other keys.
1020+
1021+
Please refer to `RunnableConfig` for more details.
10171022
return_exceptions: Whether to return exceptions instead of raising them.
10181023
**kwargs: Additional keyword arguments to pass to the `Runnable`.
10191024
@@ -1076,8 +1081,9 @@ async def abatch_as_completed(
10761081
10771082
The config supports standard keys like `'tags'`, `'metadata'` for
10781083
tracing purposes, `'max_concurrency'` for controlling how much work to
1079-
do in parallel, and other keys. Please refer to the `RunnableConfig`
1080-
for more details.
1084+
do in parallel, and other keys.
1085+
1086+
Please refer to `RunnableConfig` for more details.
10811087
return_exceptions: Whether to return exceptions instead of raising them.
10821088
**kwargs: Additional keyword arguments to pass to the `Runnable`.
10831089
@@ -1755,46 +1761,52 @@ def with_alisteners(
17551761
import time
17561762
import asyncio
17571763
1764+
17581765
def format_t(timestamp: float) -> str:
17591766
return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()
17601767
1768+
17611769
async def test_runnable(time_to_sleep: int):
17621770
print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
17631771
await asyncio.sleep(time_to_sleep)
17641772
print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")
17651773
1774+
17661775
async def fn_start(run_obj: Runnable):
17671776
print(f"on start callback starts at {format_t(time.time())}")
17681777
await asyncio.sleep(3)
17691778
print(f"on start callback ends at {format_t(time.time())}")
17701779
1780+
17711781
async def fn_end(run_obj: Runnable):
17721782
print(f"on end callback starts at {format_t(time.time())}")
17731783
await asyncio.sleep(2)
17741784
print(f"on end callback ends at {format_t(time.time())}")
17751785
1786+
17761787
runnable = RunnableLambda(test_runnable).with_alisteners(
1777-
on_start=fn_start,
1778-
on_end=fn_end
1788+
on_start=fn_start, on_end=fn_end
17791789
)
1790+
1791+
17801792
async def concurrent_runs():
17811793
await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))
17821794
1783-
asyncio.run(concurrent_runs())
1784-
Result:
1785-
on start callback starts at 2025-03-01T07:05:22.875378+00:00
1786-
on start callback starts at 2025-03-01T07:05:22.875495+00:00
1787-
on start callback ends at 2025-03-01T07:05:25.878862+00:00
1788-
on start callback ends at 2025-03-01T07:05:25.878947+00:00
1789-
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
1790-
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
1791-
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
1792-
on end callback starts at 2025-03-01T07:05:27.882360+00:00
1793-
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
1794-
on end callback starts at 2025-03-01T07:05:28.882428+00:00
1795-
on end callback ends at 2025-03-01T07:05:29.883893+00:00
1796-
on end callback ends at 2025-03-01T07:05:30.884831+00:00
17971795
1796+
asyncio.run(concurrent_runs())
1797+
# Result:
1798+
# on start callback starts at 2025-03-01T07:05:22.875378+00:00
1799+
# on start callback starts at 2025-03-01T07:05:22.875495+00:00
1800+
# on start callback ends at 2025-03-01T07:05:25.878862+00:00
1801+
# on start callback ends at 2025-03-01T07:05:25.878947+00:00
1802+
# Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
1803+
# Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
1804+
# Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
1805+
# on end callback starts at 2025-03-01T07:05:27.882360+00:00
1806+
# Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
1807+
# on end callback starts at 2025-03-01T07:05:28.882428+00:00
1808+
# on end callback ends at 2025-03-01T07:05:29.883893+00:00
1809+
# on end callback ends at 2025-03-01T07:05:30.884831+00:00
17981810
```
17991811
"""
18001812
return RunnableBinding(
@@ -1856,7 +1868,7 @@ def with_retry(
18561868
`exp_base`, and `jitter` (all `float` values).
18571869
18581870
Returns:
1859-
A new Runnable that retries the original Runnable on exceptions.
1871+
A new `Runnable` that retries the original `Runnable` on exceptions.
18601872
18611873
Example:
18621874
```python
@@ -1940,7 +1952,9 @@ def with_fallbacks(
19401952
exceptions_to_handle: A tuple of exception types to handle.
19411953
exception_key: If `string` is specified then handled exceptions will be
19421954
passed to fallbacks as part of the input under the specified key.
1955+
19431956
If `None`, exceptions will not be passed to fallbacks.
1957+
19441958
If used, the base `Runnable` and its fallbacks must accept a
19451959
dictionary as input.
19461960
@@ -1976,7 +1990,9 @@ def _generate(input: Iterator) -> Iterator[str]:
19761990
exceptions_to_handle: A tuple of exception types to handle.
19771991
exception_key: If `string` is specified then handled exceptions will be
19781992
passed to fallbacks as part of the input under the specified key.
1993+
19791994
If `None`, exceptions will not be passed to fallbacks.
1995+
19801996
If used, the base `Runnable` and its fallbacks must accept a
19811997
dictionary as input.
19821998
@@ -2442,10 +2458,14 @@ def as_tool(
24422458
24432459
`as_tool` will instantiate a `BaseTool` with a name, description, and
24442460
`args_schema` from a `Runnable`. Where possible, schemas are inferred
2445-
from `runnable.get_input_schema`. Alternatively (e.g., if the
2446-
`Runnable` takes a dict as input and the specific dict keys are not typed),
2447-
the schema can be specified directly with `args_schema`. You can also
2448-
pass `arg_types` to just specify the required arguments and their types.
2461+
from `runnable.get_input_schema`.
2462+
2463+
Alternatively (e.g., if the `Runnable` takes a dict as input and the specific
2464+
`dict` keys are not typed), the schema can be specified directly with
2465+
`args_schema`.
2466+
2467+
You can also pass `arg_types` to just specify the required arguments and their
2468+
types.
24492469
24502470
Args:
24512471
args_schema: The schema for the tool.
@@ -2514,7 +2534,7 @@ def f(x: dict[str, Any]) -> str:
25142534
as_tool.invoke({"a": 3, "b": [1, 2]})
25152535
```
25162536
2517-
String input:
2537+
`str` input:
25182538
25192539
```python
25202540
from langchain_core.runnables import RunnableLambda

libs/core/pyproject.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ requires = ["hatchling"]
33
build-backend = "hatchling.build"
44

55
[project]
6-
authors = []
6+
name = "langchain-core"
7+
description = "Building applications with LLMs through composability"
78
license = {text = "MIT"}
9+
readme = "README.md"
10+
authors = []
11+
12+
version = "1.0.3"
813
requires-python = ">=3.10.0,<4.0.0"
914
dependencies = [
1015
"langsmith>=0.3.45,<1.0.0",
@@ -15,10 +20,6 @@ dependencies = [
1520
"packaging>=23.2.0,<26.0.0",
1621
"pydantic>=2.7.4,<3.0.0",
1722
]
18-
name = "langchain-core"
19-
version = "1.0.3"
20-
description = "Building applications with LLMs through composability"
21-
readme = "README.md"
2223

2324
[project.urls]
2425
Homepage = "https://docs.langchain.com/"

libs/core/tests/unit_tests/pydantic_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _remove_additionalproperties(schema: dict) -> dict[str, Any]:
105105
generating JSON schemas for dict properties with `Any` or `object` values.
106106
107107
Pydantic 2.12 and later versions include `"additionalProperties": True` when
108-
generating JSON schemas for TypedDict.
108+
generating JSON schemas for `TypedDict`.
109109
"""
110110
if isinstance(schema, dict):
111111
if (

libs/langchain/langchain_classic/agents/agent.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ async def aplan(
105105
@property
106106
@abstractmethod
107107
def input_keys(self) -> list[str]:
108-
"""Return the input keys.
109-
110-
:meta private:
111-
"""
108+
"""Return the input keys."""
112109

113110
def return_stopped_response(
114111
self,
@@ -278,10 +275,7 @@ async def aplan(
278275
@property
279276
@abstractmethod
280277
def input_keys(self) -> list[str]:
281-
"""Return the input keys.
282-
283-
:meta private:
284-
"""
278+
"""Return the input keys."""
285279

286280
def return_stopped_response(
287281
self,
@@ -819,10 +813,7 @@ def get_full_inputs(
819813

820814
@property
821815
def input_keys(self) -> list[str]:
822-
"""Return the input keys.
823-
824-
:meta private:
825-
"""
816+
"""Return the input keys."""
826817
return list(set(self.llm_chain.input_keys) - {"agent_scratchpad"})
827818

828819
@model_validator(mode="after")
@@ -837,7 +828,7 @@ def validate_prompt(self) -> Self:
837828
838829
Raises:
839830
ValueError: If `agent_scratchpad` is not in prompt.input_variables
840-
and prompt is not a FewShotPromptTemplate or a PromptTemplate.
831+
and prompt is not a FewShotPromptTemplate or a PromptTemplate.
841832
"""
842833
prompt = self.llm_chain.prompt
843834
if "agent_scratchpad" not in prompt.input_variables:
@@ -1220,18 +1211,12 @@ def iter(
12201211

12211212
@property
12221213
def input_keys(self) -> list[str]:
1223-
"""Return the input keys.
1224-
1225-
:meta private:
1226-
"""
1214+
"""Return the input keys."""
12271215
return self._action_agent.input_keys
12281216

12291217
@property
12301218
def output_keys(self) -> list[str]:
1231-
"""Return the singular output key.
1232-
1233-
:meta private:
1234-
"""
1219+
"""Return the singular output key."""
12351220
if self.return_intermediate_steps:
12361221
return [*self._action_agent.return_values, "intermediate_steps"]
12371222
return self._action_agent.return_values

libs/langchain/langchain_classic/agents/openai_functions_agent/agent_token_buffer_memory.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ def buffer(self) -> list[BaseMessage]:
4949

5050
@property
5151
def memory_variables(self) -> list[str]:
52-
"""Always return list of memory variables.
53-
54-
:meta private:
55-
"""
52+
"""Always return list of memory variables."""
5653
return [self.memory_key]
5754

5855
@override

0 commit comments

Comments
 (0)