Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fields of pydantic can be updated by aliases #2585

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libs/langgraph/langgraph/graph/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,13 @@ def _get_state_key(input: Union[None, dict, Any], *, key: str) -> Any:
if input is None:
return SKIP_WRITE
elif isinstance(input, dict):
if hasattr(self.builder.output, 'model_fields'):
model_fields = self.builder.output.model_fields
# Extract the alias for the key if it exists
if key in model_fields:
alias = model_fields[key].alias
if alias in input:
return input.get(alias, SKIP_WRITE)
if all(k not in output_keys for k in input):
raise InvalidUpdateError(
f"Expected node {node_key} to update at least one of {output_keys}, got {input}"
Expand Down Expand Up @@ -823,6 +830,8 @@ def _get_state_reader(


def _coerce_state(schema: Type[Any], input: dict[str, Any]) -> dict[str, Any]:
if hasattr(schema, '__fields__') and any(field.alias and field.alias != name for name, field in schema.__fields__.items()):
input = {schema.model_fields[key].alias if key in schema.model_fields else key: value for key, value in input.items()}
return schema(**input)


Expand Down