core[patch]: Add unit test to cover different streaming format for json parsing (#17063)

Add unit test to cover this issue:

https://github.com/langchain-ai/langchain/issues/16423

which was resolved by this PR:

https://github.com/langchain-ai/langchain/pull/16670/files

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
pull/17338/head
Eugene Yurtsev 8 months ago committed by GitHub
parent 15bc201967
commit e10030e241
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -293,6 +293,7 @@ So
}
""".splitlines()
EXPECTED_STREAMED_JSON = [
{},
{"setup": ""},
@ -523,3 +524,58 @@ def test_raises_error() -> None:
parser = SimpleJsonOutputParser()
with pytest.raises(Exception):
parser.invoke("hi")
# A test fixture for an output which contains
# json within a code block
TOKENS_WITH_JSON_CODE_BLOCK = [
" France",
":",
"\n\n```",
"json",
"\n{",
"\n ",
' "',
"country",
"_",
"name",
'":',
' "',
"France",
'",',
" \n ",
' "',
"population",
"_",
"size",
'":',
" 67",
"39",
"15",
"82",
"\n}",
"\n```",
"\n\nI",
" looked",
" up",
]
def test_partial_text_json_output_parser_with_json_code_block() -> None:
"""Test json parser works correctly when the response contains a json code-block."""
def input_iter(_: Any) -> Iterator[str]:
for token in TOKENS_WITH_JSON_CODE_BLOCK:
yield token
chain = input_iter | SimpleJsonOutputParser()
assert list(chain.stream(None)) == [
{},
{"country_name": ""},
{"country_name": "France"},
{"country_name": "France", "population_size": 67},
{"country_name": "France", "population_size": 6739},
{"country_name": "France", "population_size": 673915},
{"country_name": "France", "population_size": 67391582},
]

Loading…
Cancel
Save