Skip to content

Incorrect pydantic example in Python SDK docs #641

@damianr13

Description

@damianr13

In the python SDK docs we have the following code snippet:

class Delivery(BaseModel):
    timestamp: datetime
    dimensions: tuple[int, int]


class CompletedDelivery(BaseModel):
    status: str
    timestamp: datetime


# For the input/output serialization of your handlers
@my_object.handler()
async def deliver(ctx: ObjectContext, delivery: Delivery) -> CompletedDelivery:

    # To serialize state
    await ctx.get("delivery", serde=PydanticJsonSerde(Delivery))
    ctx.set("delivery", delivery, serde=PydanticJsonSerde(Delivery))

    # To serialize awakeable payloads
    ctx.awakeable(serde=PydanticJsonSerde(Delivery))

    # To serialize the results of actions
    await ctx.run("some-task", some_task, serde=PydanticJsonSerde(Delivery))

    # etc.

    return CompletedDelivery(status="delivered", timestamp=datetime.now())

This is incorrect as the handler is unaware of how to deserialize the CompletedDelivery type correctly.

A relevant example for the issue is displayed further down on the page in this snippet:

# For the input/output serialization of your handlers
@my_object.handler(input_serde=MySerde(), output_serde=MySerde())
async def my_handler(ctx: ObjectContext, greeting: str) -> str:
    ...

where we learn how to pass deserializers to handlers.

So the correct pydantic example is

# For the input/output serialization of your handlers
@my_object.handler(output_serde=PydanticJsonSerde(CompletedDelivery))
async def deliver(ctx: ObjectContext, delivery: Delivery) -> CompletedDelivery:
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions