Skip to content

fix(unchecked_base_model): handle bare dict in construct_type#775

Open
devteamaegis wants to merge 1 commit into
cohere-ai:mainfrom
devteamaegis:fix/valueerror-tuple-unpacking-failure-on-em
Open

fix(unchecked_base_model): handle bare dict in construct_type#775
devteamaegis wants to merge 1 commit into
cohere-ai:mainfrom
devteamaegis:fix/valueerror-tuple-unpacking-failure-on-em

Conversation

@devteamaegis

@devteamaegis devteamaegis commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

What's broken

Calling construct_type(type_=dict, object_={...}) raises ValueError: not enough values to unpack (expected 2, got 0). Any Pydantic model field annotated as bare dict (rather than Dict[str, Any]) triggers this crash during deserialization.

Why it happens

get_args(dict) returns an empty tuple, so the two-target unpacking key_type, items_type = get_args(type_) fails when no type parameters are present.

Fix

Replace the bare unpacking with args = get_args(type_); key_type, items_type = args if args else (typing.Any, typing.Any). This makes a bare dict behave identically to Dict[Any, Any].

Test

Added tests/test_unchecked_base_model.py::test_construct_type_bare_dict_no_unpack_error which calls construct_type with type_=dict and asserts the result equals the input dict without raising.

Fixes #774


Note

Low Risk
Small defensive change in deserialization coercion plus a unit test; no auth, security, or API contract changes.

Overview
Fixes a crash in construct_type when the declared type is an unparameterized dict. get_args(dict) is empty, so unpacking into key/value element types raised ValueError during model construction/deserialization for fields typed as bare dict.

The change reads type parameters from get_args and, when none are present, treats the mapping as Dict[Any, Any] (keys and values passed through construct_type without failing). A regression test asserts construct_type(type_=dict, object_={...}) returns the input mapping unchanged.

Reviewed by Cursor Bugbot for commit e6db0dd. Bugbot is set up for automated code reviews on this repo. Configure here.

When type_ is a bare unparameterized dict, get_args(dict) returns ()
causing ValueError on tuple unpacking. Fall back to (Any, Any) when
no args are present.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit e6db0dd. Configure here.

@@ -0,0 +1,11 @@
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary sys.path hack inconsistent with other tests

Low Severity

The sys.path.insert hack is inconsistent with every other test file in this project, which all import cohere directly without path manipulation. The project uses pyproject.toml with packages = [{ include = "cohere", from = "src"}] and expects the package to be installed in the environment (via poetry install or equivalent). This workaround bypasses that and could cause subtle import-ordering issues if the installed package and source tree ever diverge.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e6db0dd. Configure here.

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.

Bug: construct_type crashes with ValueError on bare unparameterized dict

1 participant