Skip to content

Sample request: Pydantic v2 converter #97

Closed

Description

@cretz

Describe the solution you'd like

Rename sample pydantic_converter to pydanticv1_converter (and tests/packages/dep-group/etc) and make a new pydantic_converter sample that works with v2

Activity

  1. added
    enhancement
    on Nov 30, 2023
  2. changed the title [-]Sample request: Pydantic v2[/-] [+]Sample request: Pydantic v2 converter[/+] on Nov 30, 2023
  3. JaneJeon commented on Jan 24, 2024

    @JaneJeon

    I am setting up a new project and I would really appreciate this for the "type safety without having to redefine attributes twice" benefits!

    In particular, the pydantic_encoder does not work anymore in v2: pydantic/pydantic#6652

  4. cretz commented on Jan 24, 2024

    @cretz
    ContributorAuthor

    Until the sample is present, you can apply the concepts/approaches of the custom conversion to whatever is needed for v2 without applying the specific code

  5. slingshotsys commented on Jan 26, 2024

    @slingshotsys

    @JaneJeon I am using pydantic 2 with temporal just by changing pydantic_encoder to to_jsonable_python:

    from pydantic_core import to_jsonable_python
    
    ...
            # We let JSON conversion errors be thrown to caller
            return Payload(
                metadata={"encoding": self.encoding.encode()},
                data=json.dumps(value, separators=(",", ":"), sort_keys=True, default=to_jsonable_python).encode(),
            )
  6. SamZhangQingChuan commented on Dec 30, 2024

    @SamZhangQingChuan

    @JaneJeon I am using pydantic 2 with temporal just by changing pydantic_encoder to to_jsonable_python:

    from pydantic_core import to_jsonable_python
    
    ...
            # We let JSON conversion errors be thrown to caller
            return Payload(
                metadata={"encoding": self.encoding.encode()},
                data=json.dumps(value, separators=(",", ":"), sort_keys=True, default=to_jsonable_python).encode(),
            )

    I am still getting

    pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'datetime.datetime'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.`
    

    Does it work on your side?

  7. dandavison commented on Apr 9, 2025

    @dandavison
    Contributor

    The SDK now contains official Pydantic v2 support, and the sample has been updated. This is the sample to follow for Pydantic v2: https://github.com/temporalio/samples-python/tree/main/pydantic_converter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions