Skip to content

feat: add serialization to State / move State to agents.state #9345

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

Merged
merged 29 commits into from
May 23, 2025

Conversation

Amnah199
Copy link
Contributor

@Amnah199 Amnah199 commented May 5, 2025

Related Issues

Proposed Changes:

  • Introduce serialize_value and deserialize_value utility methods in the utils module. These methods encapsulate logic that is also used in the breakpoints de/serialization logic and tracing.utils.coerce_tag_value (except the json load). Once the breakpoints feature is merged in haystack, it can reuse these centralized utility functions.
  • Add serialization and deserialization logic to the State class.
  • Move the State class to the utils module, as it is not actually a data class. A deprecation warning is added to the existing State class in the dataclasses module to guide users toward the updated implementation.

How did you test it?

Moved the existing tests to test_utils_state.py and added two new tests for serialization and deserialization.

@github-actions github-actions bot added topic:tests type:documentation Improvements on the docs labels May 5, 2025
@Amnah199 Amnah199 marked this pull request as ready for review May 6, 2025 11:41
@Amnah199 Amnah199 requested review from a team as code owners May 6, 2025 11:41
@Amnah199 Amnah199 requested review from dfokina and julian-risch and removed request for a team May 6, 2025 11:41
@coveralls
Copy link
Collaborator

coveralls commented May 6, 2025

Pull Request Test Coverage Report for Build 15205617087

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 59 unchanged lines in 6 files lost coverage.
  • Overall coverage decreased (-0.1%) to 90.244%

Files with Coverage Reduction New Missed Lines %
components/agents/init.py 1 75.0%
utils/init.py 2 33.33%
components/agents/agent.py 10 91.07%
dataclasses/state_utils.py 11 0.0%
utils/base_serialization.py 16 86.99%
components/tools/tool_invoker.py 19 83.59%
Totals Coverage Status
Change from base Build 15205079200: -0.1%
Covered Lines: 11257
Relevant Lines: 12474

💛 - Coveralls

@Amnah199
Copy link
Contributor Author

Amnah199 commented May 6, 2025

I’ve kept the current haystack.dataclasses.State file for backward compatibility. However, the tests from test.dataclasses.test_state.py have been moved to utils, so we could consider removing this test file. Alternatively, we might keep it temporarily for safety until the old State class is fully removed.

@Amnah199 Amnah199 changed the title feat: add serialization to State / move State to utils feat: add serialization to State / move State to agents.state May 19, 2025
@Amnah199 Amnah199 requested a review from julian-risch May 19, 2025 13:17
@julian-risch julian-risch requested a review from sjrl May 20, 2025 07:53
@sjrl
Copy link
Contributor

sjrl commented May 20, 2025

@Amnah199 a general question for you.

While working on this did you find that we are missing to_dict and from_dict methods from dataclasses/components/etc. that would be helpful to add? For example, I noticed we don't have a from_dict or to_dict method for ByteStream.

@julian-risch julian-risch removed their request for review May 20, 2025 09:26
@Amnah199 Amnah199 requested a review from sjrl May 22, 2025 14:58
Copy link
Contributor

@sjrl sjrl left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks for all of the work on this!

@@ -54,87 +54,238 @@ class does not have a `from_dict` method.
return obj_class.from_dict(data["data"])


def serialize_value(value: Any) -> Any:
def serialize_value_with_schema(payload: Dict[str, Any]) -> Dict[str, Any]:
Copy link
Contributor

@sjrl sjrl May 22, 2025

Choose a reason for hiding this comment

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

@Amnah199 sorry last comment, should we make this method private for now so it's easy to change in the future if need be?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, until we have more confidence in this!

@Amnah199 Amnah199 merged commit 720cc19 into main May 23, 2025
20 of 21 checks passed
@Amnah199 Amnah199 deleted the add-ser-for-state-dataclass branch May 23, 2025 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic:tests type:documentation Improvements on the docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add serialization/deserialization support to State
4 participants