Skip to content

MAINT Migrate internal callers of deprecated ScenarioResult.to_dict()/from_dict() to Pydantic API#2005

Merged
rlundeen2 merged 3 commits into
microsoft:mainfrom
immu4989:fix/migrate-scenario-result-deprecations
Jun 16, 2026
Merged

MAINT Migrate internal callers of deprecated ScenarioResult.to_dict()/from_dict() to Pydantic API#2005
rlundeen2 merged 3 commits into
microsoft:mainfrom
immu4989:fix/migrate-scenario-result-deprecations

Conversation

@immu4989

Copy link
Copy Markdown
Contributor

Fixes #2004.

ScenarioResult.to_dict() and ScenarioResult.from_dict(...) are marked for removal in 0.16.0. Internal PyRIT code still calls the deprecated forms, which emits a DeprecationWarning on every GET /api/scenarios/runs/{id}/results REST call and every CLI scenario result print.

Migration

The deprecated methods are thin wrappers around the Pydantic API. ScenarioResult.to_dict() delegates to self.model_dump(mode="json", by_alias=True) and ScenarioResult.from_dict(data) delegates to cls.model_validate(data). Output shape is identical, so the change is behavior-preserving.

Changes

  • pyrit/backend/routes/scenarios.py: REST route now returns result.model_dump(mode="json", by_alias=True) and the response annotation is tightened from dict to dict[str, Any].
  • pyrit/cli/_output.py: print_scenario_result_async deserializes with ScenarioResult.model_validate(result_dict).
  • pyrit/cli/api_client.py: docstring on the matching CLI client method describes the new payload spelling.
  • tests/unit/backend/test_scenario_run_routes.py: route test mocks model_dump instead of to_dict.
  • tests/unit/cli/test_output.py: one test mocks model_validate instead of from_dict; the roundtrip integration test uses model_dump(mode="json", by_alias=True) to produce the payload.

Verification

# Targeted tests on the changed surface:
pytest tests/unit/backend tests/unit/cli
=> 906 passed in 8.18s

# Full unit suite, no regressions:
pytest tests/unit -n auto
=> 9794 passed, 5 skipped in 42.17s

# Pre-commit (ruff format, ruff check, ty type check, etc.):
pre-commit run
=> all hooks Passed

…/from_dict() to Pydantic API

ScenarioResult.to_dict() and ScenarioResult.from_dict(...) are marked for removal in 0.16.0. Internal PyRIT code still calls the deprecated forms, emitting a DeprecationWarning on every GET /api/scenarios/runs/{id}/results REST call and every CLI scenario result print.

Both deprecated methods are thin wrappers around the Pydantic API (to_dict delegates to model_dump(mode='json', by_alias=True); from_dict delegates to model_validate). Output shape is identical, so the change is behavior-preserving.

Updates the two production call sites, three docstring references, and the matching test mocks. Also tightens the REST route return annotation from dict to dict[str, Any].
Comment thread pyrit/backend/routes/scenarios.py Outdated
…FastAPI serialize

Matches the sibling cancel_scenario_run pattern (returns Pydantic model with response_model=...). Drops the manual model_dump(mode='json', by_alias=True) call and the unused 'Any' import.

Updates the route test to feed the mocked service a real ScenarioResult instance, so the response path exercises FastAPI's Pydantic serialization end-to-end instead of a mock returning a pre-shaped dict.
@rlundeen2 rlundeen2 enabled auto-merge June 16, 2026 01:43
@rlundeen2 rlundeen2 added this pull request to the merge queue Jun 16, 2026
Merged via the queue into microsoft:main with commit 05d1b0b Jun 16, 2026
53 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.

MAINT Internal callers of ScenarioResult.to_dict()/from_dict() still emit deprecation warnings

3 participants