InvokeAI/invokeai/app/services
psychedelicious 731860c332 feat(nodes): JIT graph nodes validation
We use pydantic to validate a union of valid invocations when instantiating a graph.

Previously, we constructed the union while creating the `Graph` class. This introduces a dependency on the order of imports.

For example, consider a setup where we have 3 invocations in the app:

- Python executes the module where `FirstInvocation` is defined, registering `FirstInvocation`.
- Python executes the module where `SecondInvocation` is defined, registering `SecondInvocation`.
- Python executes the module where `Graph` is defined. A union of invocations is created and used to define the `Graph.nodes` field. The union contains `FirstInvocation` and `SecondInvocation`.
- Python executes the module where `ThirdInvocation` is defined, registering `ThirdInvocation`.
- A graph is created that includes `ThirdInvocation`. Pydantic validates the graph using the union, which does not know about `ThirdInvocation`, raising a `ValidationError` about an unknown invocation type.

This scenario has been particularly problematic in tests, where we may create invocations dynamically. The test files have to be structured in such a way that the imports happen in the right order. It's a major pain.

This PR refactors the validation of graph nodes to resolve this issue:

- `BaseInvocation` gets a new method `get_typeadapter`. This builds a pydantic `TypeAdapter` for the union of all registered invocations, caching it after the first call.
- `Graph.nodes`'s type is widened to `dict[str, BaseInvocation]`. This actually is a nice bonus, because we get better type hints whenever we reference `some_graph.nodes`.
- A "plain" field validator takes over the validation logic for `Graph.nodes`. "Plain" validators totally override pydantic's own validation logic. The validator grabs the `TypeAdapter` from `BaseInvocation`, then validates each node with it. The validation is identical to the previous implementation - we get the same errors.

`BaseInvocationOutput` gets the same treatment.
2024-03-01 10:42:33 +11:00
..
board_image_records feat(db): add SQLiteMigrator to perform db migrations 2023-12-11 16:14:25 +11:00
board_images feat: refactor services folder/module structure 2023-10-12 12:15:06 -04:00
board_records feat(db): add SQLiteMigrator to perform db migrations 2023-12-11 16:14:25 +11:00
boards feat(api): chore: pydantic & fastapi upgrade 2023-10-17 14:59:25 +11:00
config final tidying before marking PR as ready for review 2024-03-01 10:42:33 +11:00
download Fix issues identified during PR review by RyanjDick and brandonrising 2024-03-01 10:42:33 +11:00
events BREAKING CHANGES: invocations now require model key, not base/type/name 2024-03-01 10:42:33 +11:00
image_files tidy(nodes): move all field things to fields.py 2024-03-01 10:42:33 +11:00
image_records tidy(nodes): move all field things to fields.py 2024-03-01 10:42:33 +11:00
images tidy(nodes): move all field things to fields.py 2024-03-01 10:42:33 +11:00
invocation_cache feat(nodes): replace latents service with tensors and conditioning services 2024-03-01 10:42:33 +11:00
invocation_processor feat(nodes): update all invocations to use new invocation context 2024-03-01 10:42:33 +11:00
invocation_queue feat: workflow library (#5148) 2023-12-09 09:48:38 +11:00
invocation_stats final tidying before marking PR as ready for review 2024-03-01 10:42:33 +11:00
item_storage revert(nodes): revert making tensors/conditioning use item storage 2024-03-01 10:42:33 +11:00
model_install final tidying before marking PR as ready for review 2024-03-01 10:42:33 +11:00
model_load final tidying before marking PR as ready for review 2024-03-01 10:42:33 +11:00
model_manager final tidying before marking PR as ready for review 2024-03-01 10:42:33 +11:00
model_metadata Fix issues identified during PR review by RyanjDick and brandonrising 2024-03-01 10:42:33 +11:00
model_records Fix issues identified during PR review by RyanjDick and brandonrising 2024-03-01 10:42:33 +11:00
names feat: refactor services folder/module structure 2023-10-12 12:15:06 -04:00
object_serializer feat(nodes): use TemporaryDirectory to handle ephemeral storage in ObjectSerializerDisk 2024-03-01 10:42:33 +11:00
session_processor feat: workflow library (#5148) 2023-12-09 09:48:38 +11:00
session_queue feat(db): add SQLiteMigrator to perform db migrations 2023-12-11 16:14:25 +11:00
shared feat(nodes): JIT graph nodes validation 2024-03-01 10:42:33 +11:00
urls feat: refactor services folder/module structure 2023-10-12 12:15:06 -04:00
workflow_records add project as category to back-end 2024-01-24 10:59:04 +11:00
__init__.py all files migrated; tweaks needed 2023-03-03 00:02:15 -05:00
invocation_services.py make model manager v2 ready for PR review 2024-03-01 10:42:33 +11:00
invoker.py feat: workflow library (#5148) 2023-12-09 09:48:38 +11:00