Skip to content

fix(optimization): give JsonField numbers and booleans typed keys - #599

Merged
liramon2 merged 5 commits into
strands-agents:mainfrom
jjbuck:opt/jsonfield-typed
Nov 24, 2026
Merged

liramon2 merged 5 commits into
strands-agents:mainfrom
jjbuck:opt/jsonfield-typed

Conversation

@jjbuck

@jjbuck jjbuck commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A JsonField on a number or a boolean was a text key whose value was the field's JSON text. Take {"temperature": 0.7, "max_turns": 2} in agent.json and @tunable(temperature=JsonField(cfg, "/temperature"), max_turns=JsonField(cfg, "/max_turns")). Then:

  • the factory received the strings '0.7' and '2', both in the plain call and from bind();
  • both keys had kind text, so the default search gave them to Reflection instead of TPE, and the score model left them out of its numeric factors;
  • no bounds applied, so max_turns accepted "2.0", "2.5", "-1" and "1e9", and "2.0" wrote "max_turns": 2.0 into the candidate's copy of the file.

This PR gives such a field a typed key:

  • A number takes the Int or Float to search it with, as a third argument: JsonField(cfg, "/temperature", Float(0.0, 1.0)) or JsonField(cfg, "/max_turns", Int(1, 20)). The file's value is the baseline, so the marker is passed without default=. A Float also takes an integer in the file, so {"top_p": 1} gives a baseline of 1.0; the baseline's copy keeps the file's bytes, and while the value is unchanged both the copy and diff() keep the file's 1.
  • A boolean needs no marker and becomes a OneOf(False, True) key.
  • A OneOf may also be given for a string, number, boolean, or null field, to search it among those options, e.g. JsonField(cfg, "/model", OneOf("small", "large")).
  • Without a marker, strings, arrays, objects, and null stay text values holding the text or the JSON text, as before. Arrays and objects still reach the factory as JSON text; this PR doesn't change that.

The candidate stores the typed value. Space.validate then applies its usual strict checks (an int key rejects 2.0 and "2", and bounds apply), the default search puts these keys in the "numbers" block with TPE, the factory receives 0.7, 2, or True, and the copy writes them as JSON numbers and booleans. Space.create_candidate_copy and the diff renderer pass a JsonField its value as is instead of str(value), which would turn True into "True".

A changed JSON number shows in its file's hunk in diff() and in changed_values().

Behavior changes (JsonField has not been released yet):

  • A JsonField on a number without a marker raises TypeError at declaration and asks for an Int or Float. Before, it declared an unbounded text key.
  • A marker raises TypeError when it has a default=, doesn't fit the file's value, isn't an Int, Float, or OneOf, is a JsonChoice (its options are JSON text, which would be written into the file as strings), or is given for an array or object. A Float over an integer beyond 2**53, where a float stops holding every integer, raises too. So does size_cap or edit_instructions with a marker or on a boolean, since nothing reads them there.
  • Values for these keys are typed, so text such as {"max_turns": "3"} passed to bind() or proposed by a custom proposer fails validation.
  • Block(Reflection(), "max_turns") on a number or boolean JsonField now raises at Search.resolve, as it does for an Int key.
  • A run started before this change with a number or boolean JsonField won't resume after it, because the space recorded in run.json differs.

Related Issues

Part of #442. Fixes behavior added in #504, which introduced JsonField.

Documentation PR

None. The optimize skill (.agents/skills/optimize/SKILL.md) is updated in this PR: how to read a typed JsonField in a factory, the Reflection warning for a text JsonField only, the declaration errors, and where a changed JsonField shows in best.diff and best.values.json.

Type of Change

Bug fix

Testing

  • New tests in tests/strands_evals/optimization/declaration/test_files.py cover int, float, and boolean keys, a Float over an integer in the file, OneOf on a string and on null, the rejection of "2.0", "2", 2.0, 2.5, True, -1, and 10**9 for an int key, the declaration errors, what the factory receives from the plain call and from bind(), the baseline copy keeping the file's bytes, and how a changed copy writes each typed key. test_blocks.py checks the default blocks, and test_candidate.py the diff (including a Float over a file's integer) and changed_values().

  • Tests that used an unbounded number JsonField now pass an Int or use an array or object instead. The 31 new typed-key cases, and the 3 cases moved to Int(1, 20) fail on the base branch and pass here. The 7 cases moved to arrays and objects pass on both.

  • Rebased onto main: hatch test tests/strands_evals/optimization/ tests/strands_evals/cli/ gives 2575 passed and 1 skipped; hatch fmt --linter (ruff and mypy) and hatch fmt --formatter --check pass.

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@jjbuck
jjbuck requested a review from a team as a code owner November 17, 2026 21:40
@github-actions github-actions Bot added bug Something isn't working area-core Core eval framework: Case, Experiment, task handler, evaluation data stores labels Nov 17, 2026
arielnabavian
arielnabavian previously approved these changes Nov 24, 2026
@nhungbi
nhungbi force-pushed the opt/jsonfield-typed branch from 8e4e0f4 to f8555ba Compare November 24, 2026 22:02
@nhungbi
nhungbi changed the base branch from workstream/optimizer-workflow to main November 24, 2026 22:02
@nhungbi
nhungbi deployed to auto-approve November 24, 2026 22:03 — with GitHub Actions Active
@nhungbi
nhungbi deployed to auto-approve November 24, 2026 22:03 — with GitHub Actions Active
@github-actions

Copy link
Copy Markdown

Assessment: Comment

The change does what it sets out to do. Numbers and booleans in a JsonField now get strictly typed int, float, or one_of keys, the default search sends them to TPE, and both the factory and the copy get real JSON values. I tried the mixed-type edge cases (0 vs OneOf(False, True), False vs OneOf(0, 1), 1.0 vs OneOf(1, 2), 2.0 vs Int), and each one is rejected at declaration as it should be. Writes keep the file's bytes when a value is unchanged. The optimization tests pass locally, and ruff check and ruff format --check pass on the changed files.

Review Categories
  • PR description vs. diff: The description says the run summary's count line changes to "Changes: N files or text values in diff()" and that test_optimizer.py covers it. Neither result.py's summary nor test_optimizer.py changes in this commit, and the "Changes:" line isn't in the codebase (it looks like main moved to a "Changed" row). Please update the description, or add the missing change if it was dropped in a rebase.
  • Public API: JsonField gains a positional marker argument and new declaration errors. It's unreleased, so this isn't breaking for users, but it is a change to a public declaration API. Consider adding the api/needs-review label, or noting why it isn't needed.
  • Testing: The coverage is broad. One copy test asserts on part of the document instead of all of it (see the inline comment).
  • Checklist: hatch run prepare and "I have reviewed and understand every line" are still unchecked.

The validation is clear and well documented, and the error messages tell the user what to do.

@github-actions

Copy link
Copy Markdown

Assessment: Comment

Re-reviewed 54cf5d7, which adds one commit since my last review (f8555ba). The new commit rejects a JsonChoice as a JsonField marker. That fix is correct: JsonChoice is a subclass of OneOf, so it got past the isinstance check, and its JSON-text options would have been written into the file as strings. The new cases test both a string field and a number field. The optimization tests (1828 passed, 2 skipped) and ruff check / ruff format --check on the two changed files pass locally.

Still open from the previous review
  • Testing: test_the_copy_writes_typed_json_fields_as_json_values still checks only part of the document (open thread).
  • Diff rendering: a Float over an integer in the file still shows in the diff as a float (open thread, suggestion only).
  • PR description: it still describes a "Changes: N files or text values in diff()" summary line and a test_optimizer.py test, and neither is in the diff. The "Behavior changes" list could also mention that a JsonChoice marker is now rejected.
  • API label / checklist: these are unchanged from the previous review.

@github-actions

Copy link
Copy Markdown

Assessment: Approve (from a code standpoint)

Re-reviewed a36e23b. It fixes both open inline threads: a Float over a JSON integer now shows as the file's integer in diff(), and the copy test checks the whole document. The _holds guard in _render also applies to text and JSON-text keys, but those render the same either way. The optimization tests pass locally (1829 passed, 2 skipped), and ruff check / ruff format --check pass on the changed files.

Remaining non-code items
  • PR description: it still mentions a "Changes: N files or text values in diff()" summary line and a test_optimizer.py test, and neither is in the diff. It could also mention the rejection of a JsonChoice marker and the fixed diff rendering.
  • Process: there's still no api/needs-review label (or a note on why the new marker argument doesn't need one), and the hatch run prepare / "reviewed every line" checkboxes are still unchecked.

Thanks for turning the feedback around quickly.

jjbuck and others added 4 commits November 24, 2026 17:53
A JsonField on a number or a boolean was a text key holding the field's
JSON text. The factory received '0.7' instead of 0.7, the default search
gave the key to Reflection rather than TPE, the score model treated it
as text, and nothing bounded it: an integer field accepted "2.0", "2.5",
"-1" and "1e9", and "2.0" was written into the file as 2.0.

A number now takes the Int or Float to search it with, as JsonField's
third argument, e.g. JsonField(cfg, "/temperature", Float(0.0, 1.0)). The
file's value fills the marker's default, so a Float also takes an
integer in the file. A boolean becomes OneOf(False, True), and a OneOf
may also be given for a string, number, boolean, or null field. The
candidate stores the typed value, so Space.validate's strict type and
bound checks apply, TPE proposes the key, the factory receives the
number or boolean, and the copy writes a JSON number or boolean.
Strings, arrays, objects, and null without a marker are unchanged.

A number without a marker now raises TypeError at declaration, as does a
marker that has a default, doesn't fit the file's value, or is given for
an array or object. Because a diff hunk can now hold only a number
change, the summary counts "files or text values" in diff().
A JsonChoice subclasses OneOf, so JsonField accepted it, but its options
are canonical JSON text: the copy wrote that text into the file as a
string ("model": "\"small\"", "t": "0.7"), even for the baseline, and
the factory received the text. A JsonField's marker applies only to a
string, number, boolean, or null field, which a OneOf of the field's
values covers, so a JsonChoice now raises TypeError at declaration.
…eger in diff()

write() keeps {"top_p": 1} as 1 in the copy when a Float key equals it,
but _render, which diff() uses, always set the typed value, so the
review diff showed "top_p": 1.0, a line in neither the file nor the
copy. _render now leaves a field that already holds the value, as
write() does.

The copy test now compares the whole document, so it also catches a
rewrite that drops or changes the untouched fields, the big integers
included.
A OneOf on a string field may also list numbers, booleans, or None, e.g.
OneOf("small", 3, None). _holds checked the text case first, so it said
a file holding 3 or null didn't hold 3 or None, and write() rewrote a
file that already had the value. The marker is now checked first.
@nhungbi
nhungbi force-pushed the opt/jsonfield-typed branch from a36e23b to 848af80 Compare November 24, 2026 22:55
@nhungbi
nhungbi deployed to auto-approve November 24, 2026 22:58 — with GitHub Actions Active
Reading a factory's markers: a JsonField with an Int or Float is a
number, with a OneOf or on a boolean a choice, otherwise a text. The
Reflection warning covers only a text JsonField. Exit code 2 names a
factory whose JsonField fails at declaration, and the deliverables say a
changed JsonField is a hunk on its JSON file in best.diff.
@liramon2
liramon2 merged commit 60869d5 into strands-agents:main Nov 24, 2026
21 checks passed
@github-actions

Copy link
Copy Markdown

Assessment: Approve (from a code standpoint)

Re-reviewed 848af80, which rebases onto the latest main; the PR's own diff is still the same 10 files. What's new since a36e23b:

  • _holds now checks a marker before the text check. A OneOf on a string field may also list 3 or None, and the file already holding that value used to fail isinstance(field, str), which made the copy rewrite (and reformat) the file. The new parametrized test covers the number, None, and string options. Comparing against value directly instead of self._json(value) is equivalent, since _json returns typed values as they are.
  • The optimize skill's SKILL.md now describes how a JsonField with an Int, Float, OneOf, or a boolean field shows in the tune row. It matches the new behavior.

The optimization tests pass on the rebased branch (1862 passed, 2 skipped), and ruff check / ruff format --check pass on the changed files.

Remaining non-code items (unchanged)
  • PR description: it still mentions a "Changes: N files or text values in diff()" summary line and a test_optimizer.py test, and neither is in the diff. It doesn't yet mention the JsonChoice rejection, the diff rendering fix, or the SKILL.md update.
  • Process: there's still no api/needs-review label (or a note on why the new marker argument doesn't need one), and the hatch run prepare / "reviewed every line" checkboxes are still unchecked.

@github-actions

Copy link
Copy Markdown

Assessment: Approve (from a code standpoint)

Re-reviewed 907375b. The only change since 848af80 is the docs commit to .agents/skills/optimize/SKILL.md; no code or test files changed. I checked each claim in it against the code:

  • Tune row: JsonField with Int/Float is a ranged number, with OneOf a choice, on a boolean true/false, and otherwise text. This matches _markers_to_params.
  • Reflection warning scope: now limited to a JsonField with no marker on a string, array, object, or null field. That's correct, since only those keys go to Reflection.
  • Exit 2: listed for a number without Int/Float or for a JsonChoice. That's correct: the TypeError raised when the agent file runs is wrapped as an EntryPointError (a ValueError), so optimize returns EXIT_BAD_INPUT. "Propose the marker fix and make it only when the user agrees" is the right guidance for a user's own factory.
  • Results: a changed JsonField number or choice appears both as a hunk in best.diff and under its key in best.values.json. This matches diff() and changed_values().
Remaining non-code items (unchanged)
  • PR description: it still mentions a "Changes: N files or text values in diff()" summary line and a test_optimizer.py test, and neither is in the diff. It doesn't yet mention the JsonChoice rejection, the diff rendering fix, the _holds marker-first fix, or the SKILL.md update.
  • Process: there's still no api/needs-review label (or a note on why the new marker argument doesn't need one), and the hatch run prepare / "reviewed every line" checkboxes are still unchecked.

This branch was successfully deployed

1 active deployment
auto-approve — 907375bf Deployed Nov 24, 2026 by nhungbi via Run integration tests #1271
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-core Core eval framework: Case, Experiment, task handler, evaluation data stores bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants