|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2026 The Dapr Authors |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +"""Native Pydantic model support in Dapr workflows and activities. |
| 13 | +
|
| 14 | +Inputs annotated with a Pydantic BaseModel are reconstructed automatically on |
| 15 | +the receiving side — no manual serialization is needed. Outputs are emitted |
| 16 | +as plain JSON so the wire format stays interop-friendly with non-Python Dapr |
| 17 | +apps. |
| 18 | +""" |
| 19 | + |
| 20 | +from dapr.ext.workflow import ( |
| 21 | + DaprWorkflowClient, |
| 22 | + DaprWorkflowContext, |
| 23 | + WorkflowActivityContext, |
| 24 | + WorkflowRuntime, |
| 25 | +) |
| 26 | +from pydantic import BaseModel |
| 27 | + |
| 28 | + |
| 29 | +class OrderRequest(BaseModel): |
| 30 | + order_id: str |
| 31 | + customer: str |
| 32 | + amount: float |
| 33 | + |
| 34 | + |
| 35 | +class OrderResult(BaseModel): |
| 36 | + order_id: str |
| 37 | + approved: bool |
| 38 | + message: str |
| 39 | + |
| 40 | + |
| 41 | +wfr = WorkflowRuntime() |
| 42 | +instance_id = 'pydantic-demo' |
| 43 | + |
| 44 | + |
| 45 | +@wfr.workflow(name='order_workflow') |
| 46 | +def order_workflow(ctx: DaprWorkflowContext, order: OrderRequest): |
| 47 | + # `order` arrives as a real OrderRequest instance — the runtime reads the |
| 48 | + # annotation and reconstructs the model from the decoded JSON automatically. |
| 49 | + if not ctx.is_replaying: |
| 50 | + print( |
| 51 | + f'[workflow] received order {order.order_id} ' |
| 52 | + f'for {order.customer} amount={order.amount}', |
| 53 | + flush=True, |
| 54 | + ) |
| 55 | + raw = yield ctx.call_activity(approve_order, input=order) |
| 56 | + # Activity results come back as a plain dict. One line turns them into a |
| 57 | + # typed instance. |
| 58 | + result = OrderResult.model_validate(raw) |
| 59 | + if not ctx.is_replaying: |
| 60 | + print( |
| 61 | + f'[workflow] activity returned approved={result.approved}', |
| 62 | + flush=True, |
| 63 | + ) |
| 64 | + return result |
| 65 | + |
| 66 | + |
| 67 | +@wfr.activity(name='approve_order') |
| 68 | +def approve_order(ctx: WorkflowActivityContext, order: OrderRequest) -> OrderResult: |
| 69 | + # Same story: `order` is already an OrderRequest instance here. |
| 70 | + print(f'[activity] approving order {order.order_id}', flush=True) |
| 71 | + if order.amount <= 100.0: |
| 72 | + return OrderResult(order_id=order.order_id, approved=True, message='auto-approved') |
| 73 | + return OrderResult(order_id=order.order_id, approved=False, message='needs review') |
| 74 | + |
| 75 | + |
| 76 | +def main(): |
| 77 | + wfr.start() |
| 78 | + client = DaprWorkflowClient() |
| 79 | + |
| 80 | + order = OrderRequest(order_id='O-100', customer='Acme', amount=42.0) |
| 81 | + client.schedule_new_workflow(workflow=order_workflow, input=order, instance_id=instance_id) |
| 82 | + state = client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30) |
| 83 | + |
| 84 | + # state.serialized_output is a JSON string — reconstruct a typed instance. |
| 85 | + output = OrderResult.model_validate_json(state.serialized_output) |
| 86 | + print( |
| 87 | + f'[client] workflow output: order_id={output.order_id} ' |
| 88 | + f'approved={output.approved} message={output.message}', |
| 89 | + flush=True, |
| 90 | + ) |
| 91 | + |
| 92 | + client.purge_workflow(instance_id) |
| 93 | + wfr.shutdown() |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + main() |
0 commit comments