Skip to content

Conversation

@Emmanuel-Arokiaraj
Copy link

Description

This PR introduces two new utility helpers, as_message_state and to_message_state,
to normalize LangChain BaseMessage objects into a consistent message-state format.
These helpers simplify handling of different return types from LLM calls (e.g.,
strings, lists, AIMessage, SystemMessage, dict forms, etc.) and ensure that
message state is returned in a structured list format.

Additionally, this PR includes accompanying unit tests validating conversions
across all supported message types.

Key additions

  • as_message_state and to_message_state utility functions
  • Normalization support for:
    • AIMessage, HumanMessage, SystemMessage
    • Raw str
    • BaseMessage
    • list of mixed message objects
    • dict containing a messages key
  • Unit tests covering typical and edge cases

Issue

Fixes #34061 (message normalization utilities discussion)

Dependencies

None

@github-actions github-actions bot added feature core Related to the package `langchain-core` and removed feature labels Nov 24, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 24, 2025

CodSpeed Performance Report

Merging #34089 will not alter performance

Comparing Emmanuel-Arokiaraj:emman/message-state-helpers (7ac5a24) with master (eb0545a)

⚠️ 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
⏩ 21 skipped1

Footnotes

  1. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

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`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: to_message_state() and as_message_state() helpers

2 participants