- 
                Notifications
    You must be signed in to change notification settings 
- Fork 749
enhancement(msgspec): create BaseStruct with __get_pydantic_core_schema__ class method and refactor MCP msgspec.Struct types to use it #6540
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
      
      
            mscolnick
  merged 13 commits into
  marimo-team:main
from
bjoaquinc:msgspec-pydantic-integration
  
      
      
   
  Sep 26, 2025 
      
    
  
     Merged
                    Changes from 10 commits
      Commits
    
    
            Show all changes
          
          
            13 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      c07b219
              
                Add pydantic compatible BaseStruct for msgspec.Struct types being use…
              
              
                bjoaquinc 30ca638
              
                Update comment
              
              
                bjoaquinc acd9593
              
                Update code to use pydantic with lazy imports and type checking
              
              
                bjoaquinc 4729dde
              
                Add pydantic as mcp dependency
              
              
                bjoaquinc 8c95a62
              
                Add copyright and annotations
              
              
                bjoaquinc 933265e
              
                remove unused import
              
              
                bjoaquinc b2f0935
              
                Change pydantic version to >2
              
              
                bjoaquinc 227c854
              
                Update CellVariableValue to VariableValue with BaseStruct
              
              
                bjoaquinc c7d836d
              
                Update VariableValue to use BaseStruct
              
              
                bjoaquinc 33dd43b
              
                Update tests to use VariableValue, correct datatype, and inherit Sess…
              
              
                bjoaquinc 8dfabce
              
                Add test for all msgspec.Struct types exposed by tools to use BaseStruct
              
              
                bjoaquinc 0874c2e
              
                Use var_value instead of VariableValue()
              
              
                bjoaquinc a34c6da
              
                Fix var name typ -> resolved_type
              
              
                bjoaquinc File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Copyright 2025 Marimo. All rights reserved. | ||
| from __future__ import annotations | ||
|  | ||
| from typing import TYPE_CHECKING, Any | ||
|  | ||
| import msgspec | ||
|  | ||
| if TYPE_CHECKING: | ||
| from pydantic import GetCoreSchemaHandler | ||
| from pydantic_core import CoreSchema | ||
|  | ||
|  | ||
| class BaseStruct(msgspec.Struct): | ||
| @classmethod | ||
| def __get_pydantic_core_schema__( | ||
| cls, source_type: Any, handler: GetCoreSchemaHandler | ||
| ) -> CoreSchema: | ||
| # Lazy import pydantic_core | ||
| from pydantic_core import core_schema | ||
|  | ||
| # Build per-field schemas | ||
| tdf: dict[str, core_schema.TypedDictField] = {} | ||
| for f in msgspec.structs.fields(cls): | ||
| tdf[f.name] = core_schema.typed_dict_field( | ||
| schema=handler.generate_schema(f.type), | ||
| required=( | ||
| f.default is msgspec.UNSET | ||
| and getattr(f, "default_factory", msgspec.UNSET) | ||
| is msgspec.UNSET | ||
| ), | ||
| ) | ||
| td = core_schema.typed_dict_schema(tdf, total=True) | ||
|  | ||
| # Create a function to convert a msgspec.Struct to a dictionary. | ||
| def to_struct(values: dict[str, Any]) -> Any: | ||
| return msgspec.convert(values, cls, from_attributes=True) | ||
|  | ||
| # Create a chain schema to validate the dictionary and convert to the msgspec.Struct. | ||
| chain = core_schema.chain_schema( | ||
| [td, core_schema.no_info_plain_validator_function(to_struct)] | ||
| ) | ||
|  | ||
| # Return the json or python schema. | ||
| return core_schema.json_or_python_schema( | ||
| json_schema=chain, | ||
| python_schema=core_schema.union_schema( | ||
| [ | ||
| core_schema.is_instance_schema(cls), # fast-path | ||
| chain, | ||
| ] | ||
| ), | ||
| serialization=core_schema.plain_serializer_function_ser_schema( | ||
| msgspec.to_builtins | ||
| ), | ||
| ) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this just be
cell_variables[var_name] = var_valueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that makes sense. Done!