Skip to content

Commit b2280fd

Browse files
authored
core[patch], langchain[patch]: fix required deps (langchain-ai#14373)
1 parent 7186fae commit b2280fd

34 files changed

+1528
-109
lines changed

.github/workflows/_all_ci.yml

+1-8
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ jobs:
4848

4949
compile-integration-tests:
5050
uses: ./.github/workflows/_compile_integration_test.yml
51-
if: ${{ inputs.working-directory != 'libs/core' }}
5251
with:
5352
working-directory: ${{ inputs.working-directory }}
5453
secrets: inherit
@@ -68,7 +67,6 @@ jobs:
6867
- "3.9"
6968
- "3.10"
7069
- "3.11"
71-
if: ${{ inputs.working-directory == 'libs/langchain' }}
7270
name: Python ${{ matrix.python-version }} extended tests
7371
defaults:
7472
run:
@@ -88,12 +86,7 @@ jobs:
8886
shell: bash
8987
run: |
9088
echo "Running extended tests, installing dependencies with poetry..."
91-
poetry install -E extended_testing
92-
93-
- name: Install langchain core editable
94-
shell: bash
95-
run: |
96-
poetry run pip install -e ../core
89+
poetry install -E extended_testing --with test
9790
9891
- name: Run extended tests
9992
run: make extended_tests

.github/workflows/_compile_integration_test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838

3939
- name: Install integration dependencies
4040
shell: bash
41-
run: poetry install --with=test_integration
41+
run: poetry install --with=test_integration,test
4242

4343
- name: Check integration tests compile
4444
shell: bash

.github/workflows/_lint.yml

+28-1
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,31 @@ jobs:
9090
- name: Analysing the code with our lint
9191
working-directory: ${{ inputs.working-directory }}
9292
run: |
93-
make lint
93+
make lint_package
94+
95+
- name: Install test dependencies
96+
# Also installs dev/lint/test/typing dependencies, to ensure we have
97+
# type hints for as many of our libraries as possible.
98+
# This helps catch errors that require dependencies to be spotted, for example:
99+
# https://github.com/langchain-ai/langchain/pull/10249/files#diff-935185cd488d015f026dcd9e19616ff62863e8cde8c0bee70318d3ccbca98341
100+
#
101+
# If you change this configuration, make sure to change the `cache-key`
102+
# in the `poetry_setup` action above to stop using the old cache.
103+
# It doesn't matter how you change it, any change will cause a cache-bust.
104+
working-directory: ${{ inputs.working-directory }}
105+
run: |
106+
poetry install --with test
107+
108+
- name: Get .mypy_cache to speed up mypy
109+
uses: actions/cache@v3
110+
env:
111+
SEGMENT_DOWNLOAD_TIMEOUT_MIN: "2"
112+
with:
113+
path: |
114+
${{ env.WORKDIR }}/.mypy_cache
115+
key: mypy-test-${{ runner.os }}-${{ runner.arch }}-py${{ matrix.python-version }}-${{ inputs.working-directory }}-${{ hashFiles(format('{0}/poetry.lock', env.WORKDIR)) }}
116+
117+
- name: Analysing the code with our lint
118+
working-directory: ${{ inputs.working-directory }}
119+
run: |
120+
make lint_tests

.github/workflows/_pydantic_compatibility.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242

4343
- name: Install dependencies
4444
shell: bash
45-
run: poetry install
45+
run: poetry install --with test
4646

4747
- name: Install langchain editable
4848
working-directory: ${{ inputs.working-directory }}

.github/workflows/templates_ci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,4 @@ jobs:
3333
./.github/workflows/_lint.yml
3434
with:
3535
working-directory: templates
36-
langchain-location: ../libs/langchain
3736
secrets: inherit

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ spell_fix:
4141
# LINTING AND FORMATTING
4242
######################
4343

44-
lint:
44+
lint lint_package lint_tests:
4545
poetry run ruff docs templates cookbook
4646
poetry run ruff format docs templates cookbook --diff
4747
poetry run ruff --select I docs templates cookbook

libs/core/Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ tests:
1515
test_watch:
1616
poetry run ptw --snapshot-update --now . -- -vv -x tests/unit_tests
1717

18+
extended_tests:
19+
poetry run pytest --only-extended $(TEST_FILE)
20+
1821

1922
######################
2023
# LINTING AND FORMATTING
@@ -24,8 +27,10 @@ test_watch:
2427
PYTHON_FILES=.
2528
lint format: PYTHON_FILES=.
2629
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/experimental --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
30+
lint_package: PYTHON_FILES=langchain_core
31+
lint_tests: PYTHON_FILES=tests
2732

28-
lint lint_diff:
33+
lint lint_diff lint_package lint_tests:
2934
./scripts/check_pydantic.sh .
3035
./scripts/check_imports.sh
3136
poetry run ruff .

libs/core/langchain_core/agents.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def messages(self) -> Sequence[BaseMessage]:
105105

106106

107107
def _convert_agent_action_to_messages(
108-
agent_action: AgentAction
108+
agent_action: AgentAction,
109109
) -> Sequence[BaseMessage]:
110110
"""Convert an agent action to a message.
111111

libs/core/langchain_core/tracers/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
"Run",
77
"RunLog",
88
"RunLogPatch",
9+
"LogStreamCallbackHandler",
910
]
1011

1112
from langchain_core.tracers.base import BaseTracer
1213
from langchain_core.tracers.evaluation import EvaluatorCallbackHandler
1314
from langchain_core.tracers.langchain import LangChainTracer
14-
from langchain_core.tracers.log_stream import RunLog, RunLogPatch
15+
from langchain_core.tracers.log_stream import (
16+
LogStreamCallbackHandler,
17+
RunLog,
18+
RunLogPatch,
19+
)
1520
from langchain_core.tracers.schemas import Run
1621
from langchain_core.tracers.stdout import ConsoleCallbackHandler

libs/core/poetry.lock

+29-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/core/pyproject.toml

+21
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,37 @@ pydantic = ">=1,<3"
1414
langsmith = "~0.0.63"
1515
tenacity = "^8.1.0"
1616
jsonpatch = "^1.33"
17+
anyio = ">=3,<5"
18+
PyYAML = ">=5.3"
19+
requests = "^2"
20+
packaging = "^23.2"
21+
jinja2 = {version = "^3", optional = true}
22+
23+
[tool.poetry.group.lint]
24+
optional = true
1725

1826
[tool.poetry.group.lint.dependencies]
1927
ruff = "^0.1.5"
2028

29+
[tool.poetry.group.typing]
30+
optional = true
31+
2132
[tool.poetry.group.typing.dependencies]
2233
mypy = "^0.991"
2334
types-pyyaml = "^6.0.12.2"
2435
types-requests = "^2.28.11.5"
36+
types-jinja2 = "^2.11.9"
37+
38+
[tool.poetry.group.dev]
39+
optional = true
2540

2641
[tool.poetry.group.dev.dependencies]
2742
jupyter = "^1.0.0"
2843
setuptools = "^67.6.1"
2944

45+
[tool.poetry.group.test]
46+
optional = true
47+
3048
[tool.poetry.group.test.dependencies]
3149
# The only dependencies that should be added are
3250
# dependencies used for running tests (e.g., pytest, freezegun, response).
@@ -43,6 +61,9 @@ pytest-asyncio = "^0.21.1"
4361
optional = true
4462
dependencies = {}
4563

64+
[tool.poetry.extras]
65+
extended_testing = ["jinja2"]
66+
4667
[tool.ruff]
4768
select = [
4869
"E", # pycodestyle

libs/core/tests/integration_tests/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pytest
2+
3+
4+
@pytest.mark.compile
5+
def test_placeholder() -> None:
6+
"""Used for compiling integration tests without running any real tests."""
7+
pass
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""Configuration for unit tests."""
2+
from importlib import util
3+
from typing import Dict, Sequence
4+
5+
import pytest
6+
from pytest import Config, Function, Parser
7+
8+
9+
def pytest_addoption(parser: Parser) -> None:
10+
"""Add custom command line options to pytest."""
11+
parser.addoption(
12+
"--only-extended",
13+
action="store_true",
14+
help="Only run extended tests. Does not allow skipping any extended tests.",
15+
)
16+
parser.addoption(
17+
"--only-core",
18+
action="store_true",
19+
help="Only run core tests. Never runs any extended tests.",
20+
)
21+
22+
23+
def pytest_collection_modifyitems(config: Config, items: Sequence[Function]) -> None:
24+
"""Add implementations for handling custom markers.
25+
26+
At the moment, this adds support for a custom `requires` marker.
27+
28+
The `requires` marker is used to denote tests that require one or more packages
29+
to be installed to run. If the package is not installed, the test is skipped.
30+
31+
The `requires` marker syntax is:
32+
33+
.. code-block:: python
34+
35+
@pytest.mark.requires("package1", "package2")
36+
def test_something():
37+
...
38+
"""
39+
# Mapping from the name of a package to whether it is installed or not.
40+
# Used to avoid repeated calls to `util.find_spec`
41+
required_pkgs_info: Dict[str, bool] = {}
42+
43+
only_extended = config.getoption("--only-extended") or False
44+
only_core = config.getoption("--only-core") or False
45+
46+
if only_extended and only_core:
47+
raise ValueError("Cannot specify both `--only-extended` and `--only-core`.")
48+
49+
for item in items:
50+
requires_marker = item.get_closest_marker("requires")
51+
if requires_marker is not None:
52+
if only_core:
53+
item.add_marker(pytest.mark.skip(reason="Skipping not a core test."))
54+
continue
55+
56+
# Iterate through the list of required packages
57+
required_pkgs = requires_marker.args
58+
for pkg in required_pkgs:
59+
# If we haven't yet checked whether the pkg is installed
60+
# let's check it and store the result.
61+
if pkg not in required_pkgs_info:
62+
try:
63+
installed = util.find_spec(pkg) is not None
64+
except Exception:
65+
installed = False
66+
required_pkgs_info[pkg] = installed
67+
68+
if not required_pkgs_info[pkg]:
69+
if only_extended:
70+
pytest.fail(
71+
f"Package `{pkg}` is not installed but is required for "
72+
f"extended tests. Please install the given package and "
73+
f"try again.",
74+
)
75+
76+
else:
77+
# If the package is not installed, we immediately break
78+
# and mark the test as skipped.
79+
item.add_marker(
80+
pytest.mark.skip(reason=f"Requires pkg: `{pkg}`")
81+
)
82+
break
83+
else:
84+
if only_extended:
85+
item.add_marker(
86+
pytest.mark.skip(reason="Skipping not an extended test.")
87+
)

libs/core/tests/unit_tests/prompts/test_few_shot.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def test_partial() -> None:
233233

234234
@pytest.mark.requires("jinja2")
235235
def test_prompt_jinja2_functionality(
236-
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]]
236+
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]],
237237
) -> None:
238238
prefix = "Starting with {{ foo }}"
239239
suffix = "Ending with {{ bar }}"
@@ -256,7 +256,7 @@ def test_prompt_jinja2_functionality(
256256

257257
@pytest.mark.requires("jinja2")
258258
def test_prompt_jinja2_missing_input_variables(
259-
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]]
259+
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]],
260260
) -> None:
261261
"""Test error is raised when input variables are not provided."""
262262
prefix = "Starting with {{ foo }}"
@@ -303,7 +303,7 @@ def test_prompt_jinja2_missing_input_variables(
303303

304304
@pytest.mark.requires("jinja2")
305305
def test_prompt_jinja2_extra_input_variables(
306-
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]]
306+
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]],
307307
) -> None:
308308
"""Test error is raised when there are too many input variables."""
309309
prefix = "Starting with {{ foo }}"

libs/core/tests/unit_tests/tracers/__init__.py

Whitespace-only changes.

libs/core/tests/unit_tests/tracers/test_imports.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"Run",
99
"RunLog",
1010
"RunLogPatch",
11+
"LogStreamCallbackHandler",
1112
]
1213

1314

libs/experimental/Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ integration_tests:
3030
PYTHON_FILES=.
3131
lint format: PYTHON_FILES=.
3232
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/experimental --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
33+
lint_package: PYTHON_FILES=langchain_experimental
34+
lint_tests: PYTHON_FILES=tests
3335

34-
lint lint_diff:
36+
lint lint_diff lint_package lint_tests:
3537
poetry run ruff .
3638
poetry run ruff format $(PYTHON_FILES) --diff
3739
poetry run ruff --select I $(PYTHON_FILES)

0 commit comments

Comments
 (0)