Skip to content

remove redundant msg normalization + align env_response api#1027

Merged
mikasenghaas merged 13 commits intomainfrom
pydantic-normalization
Mar 19, 2026
Merged

remove redundant msg normalization + align env_response api#1027
mikasenghaas merged 13 commits intomainfrom
pydantic-normalization

Conversation

@mikasenghaas
Copy link
Member

@mikasenghaas mikasenghaas commented Mar 17, 2026

Description

Reduces calls to normalize_messages to minimal set:

  • Once and unconditionally in init_state (this is safe, because not on hot path)
  • Then for all other call sites (e.g. after get_prompt_messages or env_response) we use maybe_normalize_messages which emits a warning before normalizing the message to nudge users towards using the types directly to avoid potential performance bottlenecks
  • Changed the env_response return type from Messages | str to just Messages to be consistent with the rest of the API (e.g. get_model_response, rollout, etc just operate on Messages and str -> vf.Messages conversion is an example of (undesired) conversion to custom types) and thus be handled the same way as list of dicts
  • Also fixes all our envs to use those patterns

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Test improvement

Testing

  • All existing tests pass when running uv run pytest locally.
  • New tests have been added to cover the changes

Checklist

  • My code follows the style guidelines of this project as outlined in AGENTS.md
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Additional Notes


Note

Medium Risk
Tightens the core MultiTurnEnv/Environment.get_model_response APIs to require Messages and introduces conditional normalization, which could break downstream custom envs/providers that still return raw dicts/strings or pass string prompts.

Overview
Standardizes environment and model prompting to operate on typed vf.Messages only: env_response() and get_model_response() now take/return Messages (no more str), and built-in envs (alphabet_sort, doublecheck, sentence_repeater, GymEnv) were updated to emit vf.UserMessage objects instead of raw dicts.

To cut hot-path Pydantic overhead, MultiTurnEnv now uses maybe_normalize_messages() (new helper) to only normalize when needed and logs a warning once when callers still return raw dicts/strings; supporting log_once/warning_once utilities were added, and parse_response_message() now constructs AssistantMessage directly instead of round-tripping through model_validate.

Written by Cursor Bugbot for commit cd87b2c. This will update automatically on new commits. Configure here.

@mikasenghaas mikasenghaas marked this pull request as ready for review March 17, 2026 10:34
Copy link
Member

@willccbb willccbb left a comment

Choose a reason for hiding this comment

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

Approving in advance, but let's auto-convert or hard-fail if get_prompt_messages is overridden to return dicts. would prefer auto-convert i think, writing dicts is often the more ergonomic way to do it, but fine either way (could surface a helper fn + mention in docstring?).

@mikasenghaas mikasenghaas changed the title remove redundant msg normalization remove redundant msg normalization + align env_response api Mar 18, 2026
@mikasenghaas mikasenghaas force-pushed the pydantic-normalization branch from 3b62a82 to 162b0ac Compare March 18, 2026 13:06
Copy link

@cursor cursor bot left a comment

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

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

@mikasenghaas mikasenghaas merged commit b625dfb into main Mar 19, 2026
6 checks passed
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.

3 participants