Skip to content

fix: keep scalars in union deserialization instead of crashing - #1187

Open
Chen Yufeiyang (feiiiiii5) wants to merge 2 commits into
braintrustdata:mainfrom
feiiiiii5:feiiiiii5/1126-D300
Open

Chen Yufeiyang (feiiiiii5) wants to merge 2 commits into
braintrustdata:mainfrom
feiiiiii5:feiiiiii5/1126-D300

Conversation

@feiiiiii5

Copy link
Copy Markdown
Contributor

Summary

SerializableDataClass.from_dict_deep() tried every dataclass member of a union whenever v is not None, without checking v's runtime type. PromptMessage.function_call: str | FunctionCall | None legitimately accepts a string, but FunctionCall.from_dict_deep(str) called .items() and raised AttributeError, which escaped the except TypeError. A saved/local prompt with a legacy string function_call therefore crashed parameter validation.

Fix

Dispatch to a dataclass union member only when v is a Mapping; plain strings/scalars fall through to the existing assignment. Dict and None behavior is unchanged.

Verification

  • Six new regression tests: base-red 3 failed / 3 passed; with fix 6/6 passed.
  • Combined relevant suite (7 modules, including test_parameters.py and prompt cache tests): 67 passed.
  • ruff check and ruff format --check clean.

No live _create_prompt/API path, network, or credentials used.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T08:03:06.327877Z 877d2dd PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 877d2dd223

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

],
ids=["str", "none", "dict", "dataclass"],
)
def test_prompt_message_from_dict_deep_preserves_function_call(function_call, expected):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Move the regression tests into the collected core suite

Under the CI path in .github/workflows/checks.yaml, py/noxfile.py::_run_core_tests() supplies only src/braintrust as pytest's positional file_or_dir target (pytest --help: [file_or_dir] [file_or_dir] [...]); the separate sessions under tests/ target only tests/wheel and tests/api_codegen. This file is therefore never collected by CI or make test-core, so none of the six cases protects against reintroducing the prompt-deserialization crash. Move these cases into the existing py/src/braintrust/test_serializable_data_class.py, following root AGENTS.md's “colocated test_*.py” convention.

AGENTS.md reference: AGENTS.md:L57-L57

Useful? React with 👍 / 👎.

@feiiiiii5

Copy link
Copy Markdown
Contributor Author

Addressed the Codex finding about test collection. The three regression tests were in py/tests/ and would not be collected by nox -s test_core (_run_core_tests passes only src/braintrust to pytest). They are now in src/braintrust/test_serializable_data_class.py, which CI does collect. Branch head updated to ff2e9ab; src/braintrust/test_serializable_data_class.py passes (4 tests), ruff check and ruff format --check clean. The fix in serializable_data_class.py is unchanged.

This branch has not been deployed

No deployments
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