Skip to content

Conversation

cbornet
Copy link
Collaborator

@cbornet cbornet commented Jun 20, 2025

dict() is a problematic method name as it clashes with the builtin dict used as a type annotation.
This PR replaces it with an asdict method (inspired by dataclasses).
It also fixes a few places where dict must be replaced by builtins.dict until the dict() method is removed.

Copy link

vercel bot commented Jun 20, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
langchain Ready Ready Preview Comment Sep 9, 2025 9:51am

@dosubot dosubot bot added the size:L label Jun 20, 2025
@cbornet cbornet requested a review from eyurtsev June 20, 2025 16:09
@dosubot dosubot bot added the 🤖:nit label Jun 20, 2025
Copy link

codspeed-hq bot commented Jun 20, 2025

CodSpeed WallTime Performance Report

Merging #31685 will not alter performance

Comparing cbornet:typing-dict (92e60c5) with wip-v1.0 (188c015)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched benchmarks

Copy link

codspeed-hq bot commented Jun 20, 2025

CodSpeed Instrumentation Performance Report

Merging #31685 will not alter performance

Comparing cbornet:typing-dict (92e60c5) with wip-v1.0 (188c015)

Summary

✅ 14 untouched benchmarks

@eyurtsev
Copy link
Collaborator

eyurtsev commented Jun 20, 2025

We could merge as part of the 0.4 release. Unclear yet whether this change should be made -- better not to clash with a built in for new code, but for existing code this would be weighed from a cost benefit analysis to end users

@eyurtsev eyurtsev self-assigned this Jun 20, 2025
@cbornet cbornet changed the title core: deprecate problematic dict() method feat(core): deprecate problematic dict() method Jul 13, 2025
@cbornet
Copy link
Collaborator Author

cbornet commented Jul 22, 2025

@eyurtsev candidate for 1.0 ?

@mdrxy mdrxy added this to the v1 milestone Jul 22, 2025
@mdrxy mdrxy removed the 0.4 release label Jul 22, 2025
@mdrxy mdrxy added the core Related to the package `langchain-core` label Aug 7, 2025
@mdrxy mdrxy changed the base branch from master to wip-v0.4 August 7, 2025 18:28
@mdrxy mdrxy changed the title feat(core): deprecate problematic dict() method feat(core): deprecate problematic dict() method Aug 7, 2025
@mdrxy mdrxy requested a review from Copilot August 7, 2025 18:39
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR deprecates the problematic dict() method that clashes with Python's builtin dict type annotation and replaces it with an asdict() method inspired by dataclasses. The change addresses type annotation conflicts by introducing proper deprecation warnings and updating internal usage.

Key changes:

  • Introduces asdict() method as replacement for dict() across base classes
  • Adds deprecation decorators to existing dict() methods with migration guidance
  • Updates type annotations from dict to typing.Dict where the builtin conflicts with the method name

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
libs/core/langchain_core/prompts/base.py Adds asdict() method, deprecates dict(), updates type annotations and internal usage
libs/core/langchain_core/output_parsers/base.py Adds asdict() method and deprecates dict() with proper decorator
libs/core/langchain_core/language_models/llms.py Updates type annotations, adds asdict() method, and replaces internal dict() calls
libs/core/langchain_core/language_models/chat_models.py Updates type annotations, adds asdict() method, and replaces internal usage

@mdrxy
Copy link
Collaborator

mdrxy commented Aug 7, 2025

@cbornet please review

@cbornet
Copy link
Collaborator Author

cbornet commented Aug 12, 2025

@cbornet please review

done.

@mdrxy mdrxy changed the base branch from wip-v0.4 to wip-v1.0 August 25, 2025 18:25
@cbornet
Copy link
Collaborator Author

cbornet commented Aug 30, 2025

Something wrong happened with the change of git branch 😢

@cbornet
Copy link
Collaborator Author

cbornet commented Aug 30, 2025

Something wrong happened with the change of git branch 😢

fixed it.

@model_validator(mode="before")
@classmethod
def raise_deprecation(cls, values: dict) -> Any:
def raise_deprecation(cls, values: typing.Dict) -> Any: # noqa: UP006
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're dropping 3.9.

Suggested change
def raise_deprecation(cls, values: typing.Dict) -> Any: # noqa: UP006
def raise_deprecation(cls, values: dict) -> Any:

will this work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it wouldn't.
But I did it another way to avoid the UP006 by using builtins.dict.
PTAL

raise NotImplementedError(msg)

def dict(self, **kwargs: Any) -> dict:
@deprecated("0.3.61", alternative="asdict", removal="1.0")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to update this - deprecate in 1.0, remove in 2.0

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to the package `langchain-core` v1 Issue specific to LangChain 1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants