Skip to content

fix: accept JSON schemas in structured output parser - #1125

Open
Iams4kura wants to merge 2 commits into
ScrapeGraphAI:pre/betafrom
Iams4kura:bugfix/accept-json-schema-parser-20260811t080236z
Open

Iams4kura wants to merge 2 commits into
ScrapeGraphAI:pre/betafrom
Iams4kura:bugfix/accept-json-schema-parser-20260811t080236z

Conversation

@Iams4kura

Copy link
Copy Markdown

Summary

  • Fixes: Passing a supported JSON Schema dictionary into structured-output generation raises TypeError before any model request instead of parsing the returned dictionary.
  • Root cause: get_structured_output_parser documents and types dictionaries as valid schemas but calls the class-only issubclass builtin before dispatching dictionary schemas to _dict_output_parser.

Regression evidence

  • Before: PYTHONDONTWRITEBYTECODE=1 uv run --frozen pytest -o addopts='' -p no:cacheprovider tests/utils/output_parser_test.py::test_structured_output_parser_accepts_json_schema -q exited 1

  • After: PYTHONDONTWRITEBYTECODE=1 uv run --frozen pytest -o addopts='' -p no:cacheprovider tests/utils/output_parser_test.py::test_structured_output_parser_accepts_json_schema -q exited 0

Verification

  • PYTHONDONTWRITEBYTECODE=1 .venv/bin/pytest -o addopts='' -p no:cacheprovider tests/utils/output_parser_test.py -q
  • PYTHONDONTWRITEBYTECODE=1 .venv/bin/pytest -o addopts='' -p no:cacheprovider tests/test_json_scraper_graph.py -q
  • .venv/bin/ruff check scrapegraphai/utils/output_parser.py tests/utils/output_parser_test.py
  • .venv/bin/black --check scrapegraphai/utils/output_parser.py tests/utils/output_parser_test.py
  • .venv/bin/isort --check-only scrapegraphai/utils/output_parser.py tests/utils/output_parser_test.py
  • uv build --no-sources
  • offline ChatOpenAI.with_structured_output JSON Schema contract check

Scope

  • 2 files changed, +18 / -1 lines

Local full-suite note

I also attempted the repository CI selection, uv run pytest tests/ -m "unit or not integration". It reached 95% with unrelated live-provider, browser, network, and existing assertion failures, then was interrupted when an external proxy request stalled. The deterministic affected suites listed above pass.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working tests Improvements or additions to test labels Aug 11, 2026
@VinciGit00

Copy link
Copy Markdown
Member

Thanks for this — the diagnosis holds up. I reproduced it on pre/beta: get_structured_output_parser({...}) raises TypeError: issubclass() arg 1 must be a class, so _dict_output_parser was unreachable dead code despite the signature advertising Dict[str, Any].

One note on the test before this goes in.

_dict_output_parser is just return x, so assert parser(output) == output is an identity assertion — it holds for any pass-through callable and never checks that the right parser was selected. Swap _dict_output_parser for _base_model_v2_output_parser in the dispatch and the test still passes on the returned value alone; it only fails today because get_structured_output_parser(schema) itself raises before the assertion is reached.

What actually regressed is the dispatch, so I'd assert on that directly:

from scrapegraphai.utils.output_parser import (
    _dict_output_parser,
    get_structured_output_parser,
)


def test_structured_output_parser_accepts_json_schema():
    schema = {
        "title": "Person",
        "type": "object",
        "properties": {"name": {"type": "string"}},
    }

    assert get_structured_output_parser(schema) is _dict_output_parser

That still fails with the TypeError before the fix, and afterwards it pins the behaviour the fix introduces rather than a property of the returned function. Keeping the existing parser(output) == output line alongside it is fine if you want the round-trip covered too.

Worth doing because tests/utils/output_parser_test.py is one of the files in the test-suite.yml run list, so this test is real CI coverage rather than a local-only check.

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

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files. tests Improvements or additions to test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants