[Bug] JSON fragment merging crashes when a scalar precedes a list for the same field
Description
_parse_individual_json() can raise an uncaught AttributeError before its final Pydantic validation. If an earlier JSON fragment provides a scalar for a list field and a later fragment provides a valid list, the list-merge branch calls .extend() on the earlier scalar.
Reproduction
With Agno and Pydantic installed:
from pydantic import BaseModel
from agno.utils.string import _parse_individual_json
class Answer(BaseModel):
items: list[str]
print(_parse_individual_json('{"items":"draft"}\n{"items":["final"]}', Answer))Actual: AttributeError: 'str' object has no attribute 'extend'. The same failure occurs with an earlier integer, null, boolean, or object. Expected: either discard the incompatible earlier field and validate the later list, or return the helper's documented failure value rather than raising an implementation-level exception. The narrow candidate below uses the former behavior, consistent with this function's existing last-value-wins treatment of scalar fragments.
Candidate change
In the list branch, initialize the accumulator when its current value is not a list, instead of only when the key is absent:
- if field_name not in merged_data:
+ if not isinstance(merged_data.get(field_name), list):
merged_data[field_name] = []
merged_data[field_name].extend(field_value)Searched issues/PRs for _parse_individual_json; #9305 addresses extraction of objects from prose, not this accumulator-type failure.
Feedback on the proposed recovery behavior is welcome before a PR.
Source: agno-agi/agno