Skip to content

Conversation

@arrdel
Copy link

@arrdel arrdel commented Dec 12, 2025

This PR implements the suggestion from langchain-ai/langchain#34230 to add documentation about the importance of properly defining state schema when extending existing state classes.

Changes

  • Added a warning callout in the Graph API documentation (graph-api.mdx)
  • Explains that custom fields must be explicitly defined in TypedDict when extending state schemas
  • Includes clear examples of incorrect vs correct usage
  • References issue #34108

Why This Matters

When extending state schemas like AgentState or MessagesState, developers often forget to use TypedDict to extend the schema. This silently results in custom fields being ignored in the graph state, leading to confusing bugs. This warning helps prevent this issue.

Closes langchain-ai/langchain#34108 (documentation)

When extending state schemas like AgentState or MessagesState, custom fields
must be explicitly defined in the TypedDict, otherwise they will be silently
ignored. This warning clarifies the proper way to extend state schemas with
custom fields and references issue #34108.

The warning is placed in the Schema section of the Graph API documentation
with clear examples of incorrect vs correct usage.
Copilot AI review requested due to automatic review settings December 12, 2025 15:00
@arrdel arrdel requested a review from lnhsingh as a code owner December 12, 2025 15:00
@github-actions github-actions bot added langgraph For docs changes to LangGraph oss external User is not a member of langchain-ai labels Dec 12, 2025
Copy link
Contributor

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 adds a warning callout to the Graph API documentation to help developers avoid a common pitfall when extending LangGraph state schemas. The warning addresses issue #34108 by documenting that custom fields must be explicitly defined using TypedDict when extending state classes like AgentState or MessagesState, otherwise they will be silently ignored.

Key changes:

  • Added a warning section explaining the requirement to use TypedDict for extending state schemas
  • Included code examples showing both incorrect and correct approaches
  • Referenced the related GitHub issue for additional context

Comment on lines +88 to +91
### Defining All State Keys

When extending a state schema (e.g., extending `AgentState` or `MessagesState`), you **must explicitly define all custom fields in your state TypedDict**. Any fields that are not declared in the schema definition will be silently ignored and not available in your graph.

Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The heading "Defining All State Keys" is positioned inside a warning callout. According to Mintlify documentation patterns and the examples found in this repository (e.g., line 58 uses <Warning> without a heading inside), warning callouts typically don't include headings. The heading structure could interfere with the document's table of contents and navigation.

Consider removing the heading and incorporating that information into the opening sentence of the warning, or restructure this as a regular section with the warning callout inside it.

Suggested change
### Defining All State Keys
When extending a state schema (e.g., extending `AgentState` or `MessagesState`), you **must explicitly define all custom fields in your state TypedDict**. Any fields that are not declared in the schema definition will be silently ignored and not available in your graph.
When defining all state keys: when extending a state schema (e.g., extending `AgentState` or `MessagesState`), you **must explicitly define all custom fields in your state TypedDict**. Any fields that are not declared in the schema definition will be silently ignored and not available in your graph.

Copilot uses AI. Check for mistakes.
Comment on lines +87 to +113
:::warning
### Defining All State Keys

When extending a state schema (e.g., extending `AgentState` or `MessagesState`), you **must explicitly define all custom fields in your state TypedDict**. Any fields that are not declared in the schema definition will be silently ignored and not available in your graph.

For example:

```python
from langgraph.graph import MessagesState

# ✗ WRONG - custom fields are not defined in the schema
class WrongState(MessagesState):
authenticated: bool # This field will be silently ignored!
user_id: str # This field will be silently ignored!

# ✓ CORRECT - use TypedDict to properly extend the schema
from typing_extensions import TypedDict

class CorrectState(MessagesState, TypedDict):
authenticated: bool # Properly defined
user_id: str # Properly defined
```

This applies to any custom state you create by extending existing state schemas. Always use `TypedDict` to ensure your custom fields are properly declared and will be persisted in your graph state.

See [#34108](https://github.com/langchain-ai/langgraph/issues/34108) for more context.
:::
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

This warning callout is only wrapped in Python-specific language fences (:::python would be needed), but it's placed outside of any language-specific block. Since this warning discusses Python-specific concepts (TypedDict, MessagesState), it should either be:

  1. Wrapped in :::python / ::: fences to indicate it's Python-specific, or
  2. Include a JavaScript/TypeScript equivalent example if this issue also applies to JavaScript/TypeScript users.

Looking at the context, the Schema section has both :::python and :::js blocks (lines 74-84), so this warning should follow the same pattern to maintain consistency with the documentation structure.

Copilot uses AI. Check for mistakes.

This applies to any custom state you create by extending existing state schemas. Always use `TypedDict` to ensure your custom fields are properly declared and will be persisted in your graph state.

See [#34108](https://github.com/langchain-ai/langgraph/issues/34108) for more context.
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The GitHub issue reference uses the wrong repository. The link references github.com/langchain-ai/langgraph/issues/34108, but according to the PR description, this should reference langchain-ai/langchain#34108. The issue number suggests this is in the langchain repository, not langgraph.

The link should be updated to reference the correct repository or use a full URL format if cross-repository references are needed.

Suggested change
See [#34108](https://github.com/langchain-ai/langgraph/issues/34108) for more context.
See [#34108](https://github.com/langchain-ai/langchain/issues/34108) for more context.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external User is not a member of langchain-ai langgraph For docs changes to LangGraph oss

Projects

None yet

Development

Successfully merging this pull request may close these issues.

State not preserving custom fields — only messages available in runtime.state

1 participant