mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-28 15:00:23 +00:00
This PR upgrades core to pydantic 2. It involves a combination of manual changes together with automated code mods using gritql. Changes and known issues: 1. Current models override __repr__ to be consistent with pydantic 1 (this will be removed in a follow up PR) Related: https://github.com/langchain-ai/langchain/pull/25986/files#diff-e5bd296179b7a72fcd4ea5cfa28b145beaf787da057e6d122aa76ee0bb8132c9R74 2. Issue with decorator for BaseChatModel (https://github.com/langchain-ai/langchain/pull/25986/files#diff-932bf3b314b268754ef640a5b8f52da96f9024fb81dd388dcd166b5713ecdf66R202) -- cc @baskaryan 3. `name` attribute in Base Runnable does not have a default -- was raising a pydantic warning due to override. We need to see if there's a way to fix to avoid making a breaking change for folks with custom runnables. (https://github.com/langchain-ai/langchain/pull/25986/files#diff-836773d27f8565f4dd45e9d6cf828920f89991a880c098b7511e0d3bb78a8a0dR238) 4. Likely can remove hard-coded RunnableBranch name (https://github.com/langchain-ai/langchain/pull/25986/files#diff-72894b94f70b1bfc908eb4d53f5ff90bb33bf8a4240a5e34cae48ddc62ac313aR147) 5. `model_*` namespace is reserved in pydantic. We'll need to specify `protected_namespaces` 6. create_model does not have a cached path yet 7. get_input_schema() in many places has been updated to be explicit about whether parameters are required or optional 8. injected tool args aren't picked up properly (losing type annotation) For posterity the following gritql migrations were used: ``` engine marzano(0.1) language python or { `from $IMPORT import $...` where { $IMPORT <: contains `pydantic_v1`, $IMPORT => `pydantic` }, `$X.update_forward_refs` => `$X.model_rebuild`, // This pattern still needs fixing as it fails (populate_by_name vs. // allow_populate_by_name) class_definition($name, $body) as $C where { $name <: `Config`, $body <: block($statements), $t = "", $statements <: some bubble($t) assignment(left=$x, right=$y) as $A where { or { $x <: `allow_population_by_field_name` where { $t += `populate_by_name=$y,` }, $t += `$x=$y,` } }, $C => `model_config = ConfigDict($t)`, add_import(source="pydantic", name="ConfigDict") } } ``` ``` engine marzano(0.1) language python `@root_validator(pre=True)` as $decorator where { $decorator <: before function_definition($body, $return_type), $decorator => `@model_validator(mode="before")\n@classmethod`, add_import(source="pydantic", name="model_validator"), $return_type => `Any` } ``` ``` engine marzano(0.1) language python `@root_validator(pre=False, skip_on_failure=True)` as $decorator where { $decorator <: before function_definition($body, $parameters, $return_type) where { $body <: contains bubble or { `values["$Q"]` => `self.$Q`, `values.get("$Q")` => `(self.$Q or None)`, `values.get($Q, $...)` as $V where { $Q <: contains `"$QName"`, $V => `self.$QName`, }, `return $Q` => `return self` } }, $decorator => `@model_validator(mode="after")`, // Silly work around a bug in grit // Adding Self to pydantic and then will replace it with one from typing add_import(source="pydantic", name="model_validator"), $parameters => `self`, $return_type => `Self` } ``` ``` grit apply --language python '`Self` where { add_import(source="typing_extensions", name="Self")}' ```
69 lines
2.1 KiB
Makefile
69 lines
2.1 KiB
Makefile
.PHONY: all format lint test tests test_watch integration_tests docker_tests help extended_tests
|
|
|
|
# Default target executed when no arguments are given to make.
|
|
all: help
|
|
|
|
# Define a variable for the test file path.
|
|
TEST_FILE ?= tests/unit_tests/
|
|
|
|
test:
|
|
poetry run pytest $(TEST_FILE)
|
|
|
|
tests:
|
|
poetry run pytest $(TEST_FILE)
|
|
|
|
test_watch:
|
|
poetry run ptw --snapshot-update --now . -- -vv $(TEST_FILE)
|
|
|
|
test_profile:
|
|
poetry run pytest -vv tests/unit_tests/ --profile-svg
|
|
|
|
check_imports: $(shell find langchain_core -name '*.py')
|
|
poetry run python ./scripts/check_imports.py $^
|
|
|
|
extended_tests:
|
|
poetry run pytest --only-extended $(TEST_FILE)
|
|
|
|
|
|
######################
|
|
# LINTING AND FORMATTING
|
|
######################
|
|
|
|
# Define a variable for Python and notebook files.
|
|
PYTHON_FILES=.
|
|
MYPY_CACHE=.mypy_cache
|
|
lint format: PYTHON_FILES=.
|
|
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/core --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
|
|
lint_package: PYTHON_FILES=langchain_core
|
|
lint_tests: PYTHON_FILES=tests
|
|
lint_tests: MYPY_CACHE=.mypy_cache_test
|
|
|
|
lint lint_diff lint_package lint_tests:
|
|
./scripts/lint_imports.sh
|
|
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff check $(PYTHON_FILES)
|
|
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff format $(PYTHON_FILES) --diff
|
|
[ "$(PYTHON_FILES)" = "" ] || mkdir -p $(MYPY_CACHE) && poetry run mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE)
|
|
|
|
format format_diff:
|
|
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff format $(PYTHON_FILES)
|
|
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff check --select I --fix $(PYTHON_FILES)
|
|
|
|
spell_check:
|
|
poetry run codespell --toml pyproject.toml
|
|
|
|
spell_fix:
|
|
poetry run codespell --toml pyproject.toml -w
|
|
|
|
######################
|
|
# HELP
|
|
######################
|
|
|
|
help:
|
|
@echo '----'
|
|
@echo 'format - run code formatters'
|
|
@echo 'lint - run linters'
|
|
@echo 'test - run unit tests'
|
|
@echo 'tests - run unit tests'
|
|
@echo 'test TEST_FILE=<test_file> - run all tests in file'
|
|
@echo 'test_watch - run unit tests in watch mode'
|