You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Browse filesBrowse the repository at this point in the historyBrowse files
pegah
committed
test: add coverage for Instructor-based structured output
Adds tests/test_instructor_migration.py (23 tests) covering the
reliability properties of the Instructor/Pydantic migration:
- TOCIndexItem.structure regression test (Optional[str] without
default=None is not omittable in Pydantic v2)
- add_page_offset_to_toc_json guard against offset=None
- llm_structured/llm_astructured: finish_reason truncation
detection, max_tokens threading, max_retries capped at 1,
litellm/ prefix stripping
- toc_transformer end-to-end behavior against the migrated
implementation
- AliasChoices synonym handling for reasoning fields
Also includes the llm_structured/llm_astructured implementation
changes these tests cover: switched from create() to
create_with_completion() to expose finish_reason, added max_tokens
parameter, capped max_retries at 1 (previously 3 — found to
compound failures on smaller models by feeding accumulated failed
completions back into retry context).
tests/test_issue_163.py is left unmodified. Running it against this
branch shows 10 failed / 4 passed: the 4 passes (extract_toc_content)
are unaffected since that function wasn't migrated; the 10 failures
mock llm_completion for functions now using llm_structured, so the
mock no longer intercepts the real call path. Not a regression —
a consequence of those tests asserting on manual-parsing
implementation details this PR removes. Left as-is pending
maintainer input on how to handle it.
# TODO after rebase completes — two known gaps vs. main's version, to
303
+
# resolve deliberately (see PR discussion), not silently:
304
+
# 1. No continuation/retry logic for truncated completions (main uses
305
+
# a chat-history "please continue" loop, capped at 5 attempts).
306
+
# llm_structured already fails fast on finish_reason == "length",
307
+
# which is a deliberate trade-off, not an oversight.
308
+
# 2. No equivalent of check_if_toc_transformation_is_complete — main
309
+
# catches "complete-looking but actually missing sections" output
310
+
# (finish_reason == "stop" but content silently incomplete), which
311
+
# Pydantic validation alone cannot catch (a shorter-than-expected
312
+
# list still validates fine). This gap needs an explicit decision:
313
+
# port the check, or document and accept it.
304
314
print('start toc_transformer')
305
315
init_prompt="""
306
316
You are given a table of contents, You job is to transform the whole table of content into a JSON format included table_of_contents.
307
317
308
318
structure is the numeric system which represents the index of the hierarchy section in the table of contents. For example, the first section has structure index 1, the first subsection has structure index 1.1, the second subsection has structure index 1.2, etc.
309
319
310
-
The response should be in the following JSON format:
311
-
{
312
-
table_of_contents: [
313
-
{
314
-
"structure": <structure index, "x.x.x" or None> (string),
315
-
"title": <title of the section>,
316
-
"page": <page number or None>,
317
-
},
318
-
...
319
-
],
320
-
}
321
320
You should transform the full table of contents in one go.
322
321
Directly return the final JSON structure, do not output anything else. """
323
322
324
323
prompt=init_prompt+'\n Given table of contents\n:'+_secure_doc_text(toc_content)
0 commit comments