Skip to content

Fix get() fallback for unset model fields - #704

Open
PEliet wants to merge 1 commit into
ollama:mainfrom
PEliet:fix/get-default-for-unset-fields
Open

Fix get() fallback for unset model fields#704
PEliet wants to merge 1 commit into
ollama:mainfrom
PEliet:fix/get-default-for-unset-fields

Conversation

@PEliet

@PEliet PEliet commented Jul 28, 2026

Copy link
Copy Markdown

SubscriptableBaseModel exposes mapping-style helpers, but get() used
hasattr() to decide whether a field was present. Pydantic exposes unset
optional fields as attributes with their class default, so an unset field such
as Message.content returned None instead of the fallback passed to get().
This also disagreed with __contains__ and __getitem__, which already treat
the same field as absent.

Use the model's existing membership semantics in get(). The regression test
covers both sides of the distinction: an unset optional field returns the
fallback, while a field explicitly set to None still returns None.

Validation:

  • pytest tests/test_type_serialization.py::test_subscriptable_model_get_returns_default_for_unset_field -q
  • pytest ollama/_types.py --doctest-modules -q
  • ruff check ollama/_types.py tests/test_type_serialization.py
  • ruff format --check ollama/_types.py tests/test_type_serialization.py
  • pytest ollama tests -q -k "not generate_images and not create_blob and not image_serialization_path and not image_serialization_string_path" (91 passed, 8 deselected)

The eight deselected tests use open NamedTemporaryFile handles in ways that
fail on Windows. This is the existing issue #676 and is unrelated to this
change.

Prepared with assistance from OpenAI Codex.

Signed-off-by: PEliet <2672784691@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant