invoke-ai / invoke-ai/InvokeAI

[enhancement]: Support pydantic `default_factory` in nodes

Open
#5,187 0 comments 0 reactions 0 assignees View on GitHub
enhancement nodes / workflows
Dominant language
Python
Stars
28.2k
Forks
3k
Avg merge
6d 5h
Merged PRs (30d)
19

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Contact Details

_No response_

### What should this feature add?

Pydantic fields may have a `default_factory` instead of `default` (value). This function is called every time the field is instantiated to provide it a default value. By specifying a `default_factory`, we indicate that a field should always have a value, though the fallback/default value is dynamic.

This wasn't handled very well and is deprecated in #5175.

For "stable" mutable default values, `default_factory` is unnecessary. For example, `default_factory=list` is functionally identical to `default=[]`, because [pydantic deepcopies mutable default values](https://docs.pydantic.dev/latest/concepts/models/#fields-with-non-hashable-default-values).

We should explore adding support for `default_factory`, but there are two challenges:

1) For stateful fields, the UI wants to provide a sensible default value for fields. Of course, we cannot execute a `default_factory` function in the UI. If the field is defined with `default_factory`, what value do we provide as the default?

2) The UI currently does not support resetting stateful fields to `undefined` or their default value for most fields. `ImageField`, for example, supports this. For `default_factory` to be useful for stateful fields, this needs to be implemented, else the `default_factory` will never be run for stateful fields.

It's useful today for stateless fields, though, because they will *always* use `default_factory` or `default`.

### Alternatives

_No response_

### Additional Content

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.