dispatchrun / dispatch-py

Python package to develop applications with Dispatch.
https://pypi.org/project/dispatch-py/
Apache License 2.0
54 stars 3 forks source link

Simplify registration of error/output to status mappings #166

Closed chriso closed 4 months ago

chriso commented 4 months ago

One of the responsibilities of the SDK is to categorize function outputs (return values or exceptions raised) into a Status, so that Dispatch knows a) whether to retry, and b) how to adjust the execution concurrency moving forwards.

The SDK provides two functions for registering handler functions that map base error and output (return value) classes to a Status:

register_error_type(cls: Type[Exception], handler: Callable[[Exception], Status])

register_output_type(cls: Type[Any], handler: Callable[[Any], Status])

For example usage, see https://github.com/dispatchrun/dispatch-py/blob/main/src/dispatch/integrations/openai.py

Sometimes the user simply wants to map a specific class to a specific status, and the handler function is unnecessary indirection.

This PR simplifies the interface so that users can either pass a Status directly, or (as before) a handler function that maps error/output classes to statuses. For example, the following is now accepted:

register_error_type(TimeoutError, Status.TIMEOUT)