Skip to content

fix: materialize generators in sanitize_for_json - #504

Open
Saswatsusmoy wants to merge 1 commit into
sktime:mainfrom
Saswatsusmoy:fix/490-materialize-generators-call-method
Open

fix: materialize generators in sanitize_for_json#504
Saswatsusmoy wants to merge 1 commit into
sktime:mainfrom
Saswatsusmoy:fix/490-materialize-generators-call-method

Conversation

@Saswatsusmoy

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #490.

What does this implement/fix? Explain your changes.

call_method on splitters (e.g. SlidingWindowSplitter.split with y_dataset=airline) returned success: true but result: "<generator object ...>" because generators fell through sanitize_for_json to str().

Materialize generators/iterators (and pd.Index for split_loc) so fold data is JSON. Always run sanitize_for_json on call_method results, including the to_dict path. Copy before MultiIndex column rename so we don't mutate live objects.

Does your contribution introduce a new dependency? If yes, which one?

No.

What should a reviewer concentrate their feedback on?

  • Iterator materialization in sanitize_for_json (vs only listing in call_method)
  • pd.Index handling for split_loc
  • Tests for the issue repro and native fold parity

Any other comments?

Verified with a real MCP stdio client: instantiate → call_method split → fold lists, not generator strings. make check passes locally.

PR checklist

For all contributions
  • I've added unit tests and made sure they pass locally (make check).
  • I've added the tool to the online documentation in docs/source/. (n/a — no tool surface change)
  • I've updated the existing example scripts or provided a new one to showcase how my tool works in examples/. (n/a)

Copilot AI review requested due to automatic review settings July 16, 2026 18:51

Copilot AI 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.

Pull request overview

Fixes JSON serialization of splitter outputs returned via Executor.call_method by ensuring iterator/generator results (and pd.Index) are materialized and recursively sanitized, preventing generator str() leakage (issue #490).

Changes:

  • Materialize iterators/generators (and pd.Index) inside sanitize_for_json.
  • Ensure call_method always runs sanitize_for_json on returned values, including to_dict paths, and avoid mutating live DataFrames when flattening MultiIndex columns.
  • Add tests covering iterator sanitization and splitter split* methods returning fold lists instead of generator strings.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
tests/test_sanitize.py Adds unit tests asserting generators/iterators and pd.Index are JSON-sanitized to lists.
tests/test_call_method.py Adds regression tests for splitter split/split_loc/split_series returning fold lists via call_method.
src/sktime_mcp/server.py Extends sanitize_for_json to handle iterators and pd.Index (but currently introduces an isinstance runtime error).
src/sktime_mcp/runtime/executor.py Always sanitizes call_method results and avoids mutating MultiIndex DataFrames (but currently introduces an isinstance runtime error).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sktime_mcp/server.py Outdated
Comment on lines +192 to +193
if isinstance(obj, list | tuple):
return [sanitize_for_json(item) for item in obj]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

checked this. we require python >=3.10, and isinstance accepts X | Y unions from 3.10 on. reprod fine on 3.10/3.11/3.12. sanitize and the split path still work. not a TypeError on our supported versions.

Comment thread src/sktime_mcp/server.py Outdated
Comment on lines 201 to 202
if isinstance(obj, str | int | float | bool | type(None)):
return obj

@Saswatsusmoy Saswatsusmoy Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same as prev.

Comment thread src/sktime_mcp/runtime/executor.py Outdated
) -> str | None:
"""Return a stable frequency label for metadata without assuming datetime-only indexes."""
if isinstance(index, (pd.DatetimeIndex, pd.PeriodIndex)):
if isinstance(index, pd.DatetimeIndex | pd.PeriodIndex):

@Saswatsusmoy Saswatsusmoy Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same as prev.

Comment thread src/sktime_mcp/runtime/executor.py Outdated
Comment on lines 1149 to 1150
if freq is None and isinstance(y.index, pd.DatetimeIndex | pd.PeriodIndex):
# Try to infer

@Saswatsusmoy Saswatsusmoy Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same as prev.

@Saswatsusmoy
Saswatsusmoy force-pushed the fix/490-materialize-generators-call-method branch from dc5b245 to b3d79dd Compare July 19, 2026 01:34
@Shashankss1205

Copy link
Copy Markdown
Collaborator

The generator-materialisation half landed on main in 685195e (#520), so that part is now redundant, but the rest is still valuable: on current main split_loc folds come back as PeriodIndex([...]) repr strings, which your pd.Index branch fixes, and the .copy() before the MultiIndex rename plus always sanitising the to_dict path are good catches. Could you rebase onto main, drop the duplicated generator handling and the two split tests already covered by tests/test_call_method_generators.py, keep the pd.Index/copy/always-sanitize changes with their tests, and leave the isinstance style rewrites out? Then this is a quick merge.

Generator materialization already landed in sktime#520. split_loc still
leaked PeriodIndex reprs, to_dict skipped sanitize, and MultiIndex
flatten mutated the live frame.

Fixes sktime#490
@Saswatsusmoy
Saswatsusmoy force-pushed the fix/490-materialize-generators-call-method branch from b3d79dd to c81852b Compare September 6, 2026 19:47
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]The Splitter Limitation (Generators over JSON)

3 participants