Skip to content

Redesign plan argument handling #1624

Open

Description

@tpoliaw

We currently build a pydantic model from the plan args and use that model to convert from the JSON params sent as part of a TaskRequest. This leads to several issues

  • All arguments must be addressable via keywords. Positional only args and varargs are not supported
  • Devices need special handling to convert from strings to devices Deserialise plan method arguments by replacing types instead of building custom validators #154
  • Composite devices (built at runtime from existing devices) have their handling intertwined with the device name to device handling
  • The inject method we currently use to provide default arguments to plans means plans can't be run outside blueapi but this is not restricted by python. Eg, calling a plan defined as def foo(mov: Movable = inject("bar")): ... fails with a run time error when trying to move the string instead of giving a type error. Type-checkers can't pick this up.
  • Non-devices are not supported (eg plans or clients for external services.)

Not directly related to using a model for validation but plan args are validated twice, once on submission and again when the plan is actually started.

Redesigning the way args are handled has been discussed several times and the general consensus was for the following requirements

  • We shouldn't have to change every existing plan. This would need a huge amount of churn and introduce new version dependencies
  • No new dependencies between blueapi and dodal/plan repo in either direction. A third API only package is not out of the question. (This may also be useful for utilities like the DeviceManager)
  • Devices built by a device manager should be injectable in a similar way to the current system
  • Non-devices should be available. Eg a config service client should be usable by plans without relying on global state
  • Composite devices should be built at runtime with overrides of individual components if possible
  • Type-checking should work
  • Vararg and positional only args should be supported (Raise a ValidationError for positional-only plans #1185)
  • Plan definitions should remain as simple as possible. A bit subjective but the current inject method is fairly concise.
    • If possible, less duplication than the current system would be good, eg foo: Movable = inject("foo") could be reduced to something like foo: Injected(Movable) and the name would not need to be duplicated.

Activity

  1. tpoliaw commented on Aug 18, 2026

    @tpoliaw
    ContributorAuthor

    Could possibly get some way towards supporting #1458 as well. If a plan is defined as

    def foo(x: Movable[T], pos: T): ...

    and when x is looked up, it can be shown to be an instance of Movable[SpecificPos], we should be able to use that to deserialize pos to the right type.

    This would rely on types explicitly subclassing protocols, eg

    class Special(Movable[SpecificPos]):
        ...

    instead of relying on duck typing.

  2. oliwenmandiamond commented on Aug 26, 2026

    @oliwenmandiamond
    Contributor

    Could possibly get some way towards supporting #1458 as well. If a plan is defined as

    def foo(x: Movable[T], pos: T): ...

    and when x is looked up, it can be shown to be an instance of Movable[SpecificPos], we should be able to use that to deserialize pos to the right type.

    This would rely on types explicitly subclassing protocols, eg

    class Special(Movable[SpecificPos]):
    ...

    instead of relying on duck typing.

    I think this is a move in the right direction as it will help with most devices but I am not sure it is overall the best approach.
    Lets say I have a Movable device where the type is CustomT. There's now nothing for blueapi to use as T is not a concrete type and again we get the same issue. Also, how would this work with sub classes? I already have a use case

    @devices.factory()
    def ew4000(
        dual_energy_source: DualEnergySource, source_selector: SignalRW[SelectedSource]
    ) -> VGScientaDetector[LensMode, PsuMode, PassEnergy]:
        prefix = f"{I_PREFIX.beamline_prefix}-EA-DET-01:CAM:"
        driver = VGScientaAnalyserDriverIO(prefix, LensMode, PsuMode, PassEnergy)
        return VGScientaDetector[LensMode, PsuMode, PassEnergy](
            prefix,
            driver,
            acquire_logic=ADAcquireLogic(driver),
            trigger_logic=ElectronAnalyserTriggerLogic(driver),
            region_logic=RegionLogic(driver, dual_energy_source.energy, source_selector),
        )

    The subclassing signature is as follows:

    class VGScientaDetector(
        ElectronAnalyserDetector[
            VGScientaAnalyserDriverIO[TLensMode, TPsuMode, TPassEnergy],
            VGScientaRegion[TLensMode, TPassEnergy],
        ],
        Generic[TLensMode, TPsuMode, TPassEnergy],
    ):
    class ElectronAnalyserDetector(
        AreaDetector[TAbstractAnalyserDriverIO],
        Movable[TBaseRegion],
        Generic[TAbstractAnalyserDriverIO, TBaseRegion],
    ):

    So can we guarantee that this method will resolve the type to be VGScientaRegion[T, T, T] with the runtime annotations of LensMode, PsuMode, and PassEnergy? I have a feeling it probably won't like the annotations for T here as T is not a concrete type and again we get the same issue (unless we can get the information at runtime rather than just the static type information).

    If it can't do the above, then I don't think the data should not be guessed and be forced into a specific type by inferring it via plan or device. The data on serialisation must be preserved so when it comes to deserialisation, the data and the type is the same as it was before. The only way to do this is to add the type information on serialisation so when blueapi tries to deserialise, it doesn't have to guess or infer and therefore have no edge cases.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions