Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sfc-gh-tteixeira
Copy link
Contributor

Describe your changes

Today, when you pass a Pydantic model to a cached function, you need to pass an explicit hash_func for it to be handled correctly.

With this change we now special-case Pydantic inside hashing.py to convert them to JSON, so hash_funcs aren't needed.

GitHub Issue Link (if applicable)

#10348

Testing Plan

  • Explanation of why no additional tests are needed
  • ✅ Unit Tests (JS and/or Python)
  • E2E Tests
  • Any manual testing needed?

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-12137/streamlit-1.48.1-py3-none-any.whl
🕹️ Preview app pr-12137.streamlit.app (☁️ Deploy here if not accessible)

@snyk-io
Copy link
Contributor

snyk-io bot commented Aug 6, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
@sfc-gh-tteixeira sfc-gh-tteixeira enabled auto-merge (squash) August 6, 2025 19:31

def test_pydantic_model(self):
"""Test that Pydantic models are properly hashed.

Copy link
Contributor

Choose a reason for hiding this comment

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

There's a trailing whitespace at the end of this line in the docstring. Consider removing it for consistency.

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

@lukasmasuch lukasmasuch requested a review from Copilot August 6, 2025 20:17
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 explicit support for Pydantic models in Streamlit's caching system by allowing them to be hashed without requiring custom hash_func parameters. This improves the user experience by removing the need for manual hash function configuration when caching functions that accept Pydantic models.

Key Changes:

  • Added Pydantic model detection and hashing logic in hashing.py
  • Comprehensive test coverage for Pydantic model hashing scenarios
  • Added Pydantic as a test dependency

Reviewed Changes

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

File Description
lib/streamlit/runtime/caching/hashing.py Implements Pydantic model detection and JSON-based hashing logic
lib/tests/streamlit/runtime/caching/hashing_test.py Adds comprehensive tests for Pydantic model hashing behavior
lib/test-requirements.txt Adds Pydantic as a test dependency

Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

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

LGTM 👍

One nit: pydantic is currently part of our integration unit tests. But I think its also fine to run this as part of our main unit tests since pydantic is very common as dependency. Two options:

  1. Add @pytest.mark.require_integration to the unit test and remove pydantic from test-requirements.txt. Might be a bit easier to do since our py-min-deps-test complains -> which is probably related to pydantic 2.0 being installed during the test.
  2. Remove pydantic from integration-requirements.txt and remove the @pytest.mark.require_integration from test_is_pydantic_model test.

I think you also have to run ruff formatting, or just make autofix.

Co-authored-by: Copilot <[email protected]>
@lukasmasuch lukasmasuch added security-assessment-completed Security assessment has been completed for PR change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Aug 15, 2025
@sfc-gh-tteixeira sfc-gh-tteixeira merged commit 196d1e5 into develop Aug 15, 2025
38 of 39 checks passed
@sfc-gh-tteixeira sfc-gh-tteixeira deleted the pydantic-hash branch August 15, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants