fix(core): support pydantic v2 on Python 3.14#1585
Open
phall1 wants to merge 2 commits intolangfuse:mainfrom
Open
fix(core): support pydantic v2 on Python 3.14#1585phall1 wants to merge 2 commits intolangfuse:mainfrom
phall1 wants to merge 2 commits intolangfuse:mainfrom
Conversation
f668803 to
dfa93fc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pydantic.v1imports inlangfuse/api/core/pydantic_utilities.pywhen running under Pydantic v2Serializabletype soimport langfusedoes not eagerly trigger optional dependency import warnings on Python 3.14python -W error::UserWarning -c "import langfuse"Issue
Fixes langfuse/langfuse#12626
Verification
python -m pytest tests/test_pydantic_compat.py tests/test_json.py tests/test_serializer.py -qpython -W error::UserWarning -c "import langfuse"Disclaimer: Experimental PR review
Greptile Summary
This PR fixes two import-time issues that broke
import langfuseon Python 3.14 under Pydantic v2: it eliminates the use ofpydantic.v1.*compatibility shims (which no longer exist in Python 3.14's Pydantic v2 build) and lazy-loads the optional LangChainSerializabletype to prevent transitivepydantic.v1imports from triggeringUserWarning-as-error on Python 3.14.Key changes:
langfuse/api/core/pydantic_utilities.py: Under Pydantic v2, allpydantic.v1.*imports are replaced with native equivalents —FieldInfoforModelField,typing_extensions.get_args/get_originfor type introspection,pydantic.TypeAdapterfor date/datetime parsing, and inline functions foris_literal_type/is_union. The emptyencoders_by_type = {}is intentional since Pydantic v2 handles its own JSON encoding.langfuse/_utils/serializer.py: The eager top-level import oflangchain_core.load.serializable.Serializableis replaced with a lazylru_cache-backed helper_get_langchain_serializable_type(), preventing import-time side effects from the optional dependency.tests/test_pydantic_compat.py: Adds a subprocess-based regression test that verifiesimport langfusedoes not emit anyUserWarningunder-W error::UserWarning, plus a correctness test for the newparse_date/parse_datetimeimplementations.Minor notes:
_get_langchain_serializable_typeis a deliberate trade-off for lazy loading, but it goes against the project's convention of top-level imports.pydantic.TypeAdapter(dt.date/dt.datetime)is re-instantiated on everyparse_date/parse_datetimecall; caching these at module level would be slightly more efficient.Confidence Score: 5/5
Safe to merge — all changes are correct fixes for a real Python 3.14 / Pydantic v2 compatibility issue with no regressions identified.
All remaining findings are P2 (style/performance suggestions): one in-function import that violates the project convention but is intentionally justified by the lazy-load requirement, and one minor TypeAdapter re-instantiation pattern. No logic errors, data-integrity issues, or regressions were found. The types.UnionType usage in is_union is safe because pyproject.toml enforces python >= 3.10. The new tests directly cover the fixed scenarios.
No files require special attention; all three changed files are straightforward and well-targeted.
Important Files Changed
Serializablewith a lazylru_cache-backed helper; fixes Python 3.14 import-timeUserWarningcaused by transitivepydantic.v1usage inlangchain_core. Logic is correct; one style note about in-function import per project convention.pydantic.v1.*imports under Pydantic v2, replacing them with native Pydantic v2 /typing_extensionsequivalents.ModelFieldaliased toFieldInfo,encoders_by_typeset to empty dict (intentional), andis_unioncorrectly includestypes.UnionType(safe becausepython >= 3.10is enforced in pyproject.toml). Minor:TypeAdapterre-created on eachparse_date/parse_datetimecall.import langfuseunder-W error::UserWarningcondition and the correctness ofparse_date/parse_datetimeon Pydantic v2. Well-structured and targeted.Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A["import langfuse"] --> B{IS_PYDANTIC_V2?} B -- "Yes (v2 path)" --> C["pydantic_utilities.py\nuse native Pydantic v2 APIs\n(FieldInfo, TypeAdapter, typing_extensions)"] B -- "No (v1 path)" --> D["pydantic_utilities.py\npydantic v1 imports\n(ModelField, ENCODERS_BY_TYPE, etc.)"] A --> E["serializer.py\nEventSerializer.default(obj)"] E --> F{Is obj a LangChain Serializable?} F -- "Lazy lookup (lru_cache)" --> G["_get_langchain_serializable_type()\ntry: from langchain_core...Serializable\nexcept: return None"] G -- "langchain_core present" --> H["return Serializable class\n→ obj.to_json()"] G -- "not installed" --> I["return None\n→ skip branch"] F -- "type cached after first call" --> J["isinstance check using cached type"]Reviews (1): Last reviewed commit: "fix(serializer): lazy-load langchain Ser..." | Re-trigger Greptile
(4/5) You can add custom instructions or style guidelines for the agent here!
Context used:
Learnt From
langfuse/langfuse-python#1387