|
3 | 3 | from pydantic import BaseModel, RootModel |
4 | 4 |
|
5 | 5 | from uipath_langchain.agent.react.json_utils import ( |
| 6 | + coerce_json_strings, |
6 | 7 | extract_values_by_paths, |
7 | 8 | get_json_paths_by_type, |
8 | 9 | ) |
@@ -323,3 +324,148 @@ def test_underscore_field_list_jsonpath(self) -> None: |
323 | 324 | model = create_model(schema) |
324 | 325 | paths = get_json_paths_by_type(model, "__Job_attachment") |
325 | 326 | assert paths == ["$._files[*]"] |
| 327 | + |
| 328 | + |
| 329 | +# -- coerce_json_strings: no schema (blind coercion) -------------------------- |
| 330 | + |
| 331 | + |
| 332 | +class TestCoerceJsonStringsNoSchema: |
| 333 | + """Without a schema, all parseable strings are coerced.""" |
| 334 | + |
| 335 | + def test_no_coercion_needed(self) -> None: |
| 336 | + data = {"name": "test", "count": 42} |
| 337 | + assert coerce_json_strings(data) == data |
| 338 | + |
| 339 | + def test_json_object_string(self) -> None: |
| 340 | + data = {"metadata": '{"size": "99353"}'} |
| 341 | + assert coerce_json_strings(data) == {"metadata": {"size": "99353"}} |
| 342 | + |
| 343 | + def test_python_repr_string(self) -> None: |
| 344 | + data = {"metadata": "{'size': '99353'}"} |
| 345 | + assert coerce_json_strings(data) == {"metadata": {"size": "99353"}} |
| 346 | + |
| 347 | + def test_nested_in_dict(self) -> None: |
| 348 | + data = {"attachment": {"metadata": '{"size": 1024}', "name": "file.pdf"}} |
| 349 | + assert coerce_json_strings(data) == { |
| 350 | + "attachment": {"metadata": {"size": 1024}, "name": "file.pdf"} |
| 351 | + } |
| 352 | + |
| 353 | + def test_in_list_items(self) -> None: |
| 354 | + data = { |
| 355 | + "items": [ |
| 356 | + {"metadata": '{"size": 100}', "name": "a.pdf"}, |
| 357 | + {"metadata": {"size": 200}, "name": "b.pdf"}, |
| 358 | + ] |
| 359 | + } |
| 360 | + assert coerce_json_strings(data) == { |
| 361 | + "items": [ |
| 362 | + {"metadata": {"size": 100}, "name": "a.pdf"}, |
| 363 | + {"metadata": {"size": 200}, "name": "b.pdf"}, |
| 364 | + ] |
| 365 | + } |
| 366 | + |
| 367 | + def test_invalid_string_unchanged(self) -> None: |
| 368 | + data = {"metadata": "not valid json"} |
| 369 | + assert coerce_json_strings(data) == data |
| 370 | + |
| 371 | + def test_json_array_string(self) -> None: |
| 372 | + data = {"tags": "[1, 2, 3]"} |
| 373 | + assert coerce_json_strings(data) == {"tags": [1, 2, 3]} |
| 374 | + |
| 375 | + def test_plain_string_unchanged(self) -> None: |
| 376 | + data = {"name": "hello world"} |
| 377 | + assert coerce_json_strings(data) == data |
| 378 | + |
| 379 | + def test_empty_dict(self) -> None: |
| 380 | + assert coerce_json_strings({}) == {} |
| 381 | + |
| 382 | + def test_dict_value_unchanged(self) -> None: |
| 383 | + data = {"metadata": {"already": "a dict"}} |
| 384 | + assert coerce_json_strings(data) == data |
| 385 | + |
| 386 | + def test_json_primitives_unchanged(self) -> None: |
| 387 | + """JSON primitives (numbers, booleans) stay as strings.""" |
| 388 | + data = {"value": "42", "flag": "true"} |
| 389 | + assert coerce_json_strings(data) == data |
| 390 | + |
| 391 | + def test_non_dict_passthrough(self) -> None: |
| 392 | + assert coerce_json_strings(42) == 42 |
| 393 | + assert coerce_json_strings(None) is None |
| 394 | + assert coerce_json_strings(True) is True |
| 395 | + |
| 396 | + |
| 397 | +# -- coerce_json_strings: with schema ----------------------------------------- |
| 398 | + |
| 399 | + |
| 400 | +class TestCoerceJsonStringsWithSchema: |
| 401 | + """With a schema, str-typed fields are protected from coercion.""" |
| 402 | + |
| 403 | + def test_str_field_preserved_dict_field_coerced(self) -> None: |
| 404 | + """The real-world Analyze_Files scenario.""" |
| 405 | + |
| 406 | + class AttachmentInput(BaseModel): |
| 407 | + ID: str |
| 408 | + FullName: str |
| 409 | + MimeType: str |
| 410 | + Metadata: dict[str, Any] | None = None |
| 411 | + |
| 412 | + class AnalyzeFilesInput(BaseModel): |
| 413 | + analysisTask: str |
| 414 | + attachments: list[AttachmentInput] |
| 415 | + |
| 416 | + data = { |
| 417 | + "analysisTask": '{"instruction": "summarize the document"}', |
| 418 | + "attachments": [ |
| 419 | + { |
| 420 | + "ID": "550e8400-e29b-41d4-a716-446655440000", |
| 421 | + "FullName": "report.pdf", |
| 422 | + "MimeType": "application/pdf", |
| 423 | + "Metadata": '{"size": "99353"}', |
| 424 | + } |
| 425 | + ], |
| 426 | + } |
| 427 | + result = coerce_json_strings(data, AnalyzeFilesInput) |
| 428 | + |
| 429 | + assert result["attachments"][0]["Metadata"] == {"size": "99353"} |
| 430 | + assert isinstance(result["analysisTask"], str) |
| 431 | + assert result["analysisTask"] == '{"instruction": "summarize the document"}' |
| 432 | + |
| 433 | + def test_python_repr_with_schema(self) -> None: |
| 434 | + """Single-quoted Python repr is coerced for dict fields.""" |
| 435 | + |
| 436 | + class Inner(BaseModel): |
| 437 | + Metadata: dict[str, Any] | None = None |
| 438 | + |
| 439 | + class Outer(BaseModel): |
| 440 | + item: Inner |
| 441 | + |
| 442 | + data = {"item": {"Metadata": "{'size': '99353'}"}} |
| 443 | + result = coerce_json_strings(data, Outer) |
| 444 | + assert result["item"]["Metadata"] == {"size": "99353"} |
| 445 | + |
| 446 | + def test_unknown_field_coerced(self) -> None: |
| 447 | + """Fields not in the schema fall back to blind coercion.""" |
| 448 | + |
| 449 | + class Schema(BaseModel): |
| 450 | + name: str |
| 451 | + |
| 452 | + data = {"name": "test", "extra": '{"a": 1}'} |
| 453 | + result = coerce_json_strings(data, Schema) |
| 454 | + assert result["name"] == "test" |
| 455 | + assert result["extra"] == {"a": 1} |
| 456 | + |
| 457 | + def test_nested_model_field_recurses(self) -> None: |
| 458 | + """BaseModel-typed fields recurse with child schema.""" |
| 459 | + |
| 460 | + class Child(BaseModel): |
| 461 | + value: str |
| 462 | + data: dict[str, Any] | None = None |
| 463 | + |
| 464 | + class Parent(BaseModel): |
| 465 | + child: Child |
| 466 | + |
| 467 | + result = coerce_json_strings( |
| 468 | + {"child": {"value": '{"x": 1}', "data": '{"y": 2}'}}, Parent |
| 469 | + ) |
| 470 | + assert result["child"]["value"] == '{"x": 1}' |
| 471 | + assert result["child"]["data"] == {"y": 2} |
0 commit comments