|
48 | 48 | from opentelemetry import trace |
49 | 49 | from opentelemetry.metrics import Histogram, NoOpHistogram |
50 | 50 | from opentelemetry.util.types import AttributeValue |
51 | | -from pydantic import BaseModel, Field, PlainSerializer, TypeAdapter, ValidationError, WrapSerializer, create_model |
| 51 | +from pydantic import BaseModel, Field, TypeAdapter, ValidationError, create_model |
52 | 52 | from pydantic_core import to_jsonable_python |
53 | 53 |
|
54 | 54 | from ._serialization import SerializationMixin |
@@ -506,32 +506,31 @@ def _annotation_includes_function_invocation_context(annotation: Any) -> bool: |
506 | 506 | ) |
507 | 507 |
|
508 | 508 |
|
509 | | -def _has_field_serializer(model_type: type[BaseModel], name: str) -> bool: |
510 | | - """Whether a model field has its own serializer, from a decorator or an annotation.""" |
511 | | - decorators = model_type.__pydantic_decorators__.field_serializers.values() |
512 | | - if any(name in decorator.info.fields or "*" in decorator.info.fields for decorator in decorators): |
513 | | - return True |
514 | | - field = model_type.model_fields.get(name) |
515 | | - return field is not None and any( |
516 | | - isinstance(metadata, (PlainSerializer, WrapSerializer)) for metadata in field.metadata |
517 | | - ) |
518 | | - |
| 509 | +def _escape_unencodable(value: Any) -> Any: |
| 510 | + """Copy a value with the parts JSON mode can't encode replaced, inside models, dicts and sequences too. |
519 | 511 |
|
520 | | -def _escape_invalid_utf8(value: Any) -> Any: |
521 | | - """Replace bytes that aren't valid UTF-8 with their str() form, inside models, dicts and sequences too.""" |
| 512 | + Bytes that aren't valid UTF-8 become their str() form. On Pydantic versions without ``fallback``, |
| 513 | + values JSON mode doesn't know become str() too. Models are copied, not rebuilt, so their class stays |
| 514 | + the same and the parent's declared field types still decide how they are serialized. |
| 515 | + """ |
522 | 516 | if isinstance(value, bytes): |
523 | 517 | try: |
524 | 518 | value.decode("utf-8") |
525 | 519 | except UnicodeDecodeError: |
526 | 520 | return str(value).encode() |
527 | 521 | return value |
528 | 522 | if isinstance(value, BaseModel): |
529 | | - return value.model_copy(update={name: _escape_invalid_utf8(item) for name, item in value.__dict__.items()}) |
| 523 | + return value.model_copy(update={name: _escape_unencodable(item) for name, item in value.__dict__.items()}) |
530 | 524 | if isinstance(value, dict): |
531 | 525 | dict_value = cast(dict[object, object], value) |
532 | | - return {key: _escape_invalid_utf8(item) for key, item in dict_value.items()} |
| 526 | + return {key: _escape_unencodable(item) for key, item in dict_value.items()} |
533 | 527 | if type(value) in (list, tuple, set, frozenset): |
534 | | - return type(value)(_escape_invalid_utf8(item) for item in value) |
| 528 | + return type(value)(_escape_unencodable(item) for item in value) |
| 529 | + if not _MODEL_DUMP_JSON_FALLBACK: |
| 530 | + try: |
| 531 | + to_jsonable_python(value) |
| 532 | + except ValueError: |
| 533 | + return str(value) |
535 | 534 | return value |
536 | 535 |
|
537 | 536 |
|
@@ -1414,71 +1413,41 @@ def _make_dumpable(value: Any) -> Any: |
1414 | 1413 |
|
1415 | 1414 | @staticmethod |
1416 | 1415 | def _dump_model(model: BaseModel) -> Any: |
1417 | | - """Dump a Pydantic model in JSON mode, falling back to python mode only for the values that need it. |
| 1416 | + """Dump a Pydantic model in JSON mode. |
1418 | 1417 |
|
1419 | 1418 | JSON mode applies enum, datetime and UUID encoding and JSON-only field serializers. When it |
1420 | 1419 | fails (bytes that aren't valid UTF-8, or an arbitrary type on Pydantic versions without |
1421 | | - ``fallback``), each field is dumped in JSON mode on its own and only a failing field falls |
1422 | | - back. Models inside a failing field, directly or in a list or dict, are dumped the same way, |
1423 | | - so their JSON serializers and excluded fields still apply. A failing field with its own |
1424 | | - serializer is dumped in JSON mode again with its invalid UTF-8 bytes escaped, so the |
1425 | | - serializer still runs, and is left out if that fails too. Excluded fields stay out either |
1426 | | - way, unlike the str(result) fallback in invoke. |
| 1420 | + ``fallback``), those values are escaped in a copy of the model and Pydantic dumps the copy, so |
| 1421 | + the declared field types, serializers and exclusions still decide what is written. Computed |
| 1422 | + fields are dumped from the original model. A field that still can't be encoded is left out. |
| 1423 | + Excluded fields stay out either way, unlike the str(result) fallback in invoke. |
1427 | 1424 | """ |
1428 | 1425 | try: |
1429 | 1426 | return model.model_dump(mode="json", **_MODEL_DUMP_JSON_FALLBACK) |
1430 | 1427 | except ValueError: |
1431 | 1428 | pass |
| 1429 | + escaped = _escape_unencodable(model) |
1432 | 1430 | # The python-mode dump gives the serialized field names without the excluded ones. The class's |
1433 | 1431 | # model_computed_fields can't be used for that, it's a property on Pydantic 2.0. |
1434 | 1432 | python_dump = model.model_dump(by_alias=False) |
1435 | 1433 | if not isinstance(python_dump, dict): |
1436 | | - return model.model_dump() |
1437 | | - model_type = type(model) |
| 1434 | + try: |
| 1435 | + return escaped.model_dump(mode="json", **_MODEL_DUMP_JSON_FALLBACK) |
| 1436 | + except ValueError: |
| 1437 | + logger.warning("Leaving %s out of the tool result: it can't be encoded as JSON.", type(model).__name__) |
| 1438 | + return None |
| 1439 | + fields = type(model).model_fields |
1438 | 1440 | dumped: dict[str, Any] = {} |
1439 | 1441 | for name in python_dump: |
| 1442 | + source = escaped if name in fields else model |
1440 | 1443 | try: |
1441 | | - dumped.update(model.model_dump(mode="json", include={name}, **_MODEL_DUMP_JSON_FALLBACK)) |
1442 | | - continue |
| 1444 | + dumped.update(source.model_dump(mode="json", include={name}, **_MODEL_DUMP_JSON_FALLBACK)) |
1443 | 1445 | except ValueError: |
1444 | | - pass |
1445 | | - value = getattr(model, name, None) |
1446 | | - if _has_field_serializer(model_type, name): |
1447 | | - # The python-mode value would skip the field's serializer, and with it any redaction it does. |
1448 | | - escaped = model.model_copy(update={name: _escape_invalid_utf8(value)}) |
1449 | | - try: |
1450 | | - dumped.update(escaped.model_dump(mode="json", include={name}, **_MODEL_DUMP_JSON_FALLBACK)) |
1451 | | - except ValueError: |
1452 | | - logger.warning( |
1453 | | - "Leaving %s.%s out of the tool result: its serializer's output can't be encoded as JSON.", |
1454 | | - model_type.__name__, |
1455 | | - name, |
1456 | | - ) |
1457 | | - continue |
1458 | | - for key, python_value in model.model_dump(include={name}).items(): |
1459 | | - if isinstance(value, (BaseModel, dict, list, tuple, set, frozenset)): |
1460 | | - dumped[key] = FunctionTool._dump_fallback_value(value) |
1461 | | - else: |
1462 | | - dumped[key] = python_value |
| 1446 | + logger.warning( |
| 1447 | + "Leaving %s.%s out of the tool result: it can't be encoded as JSON.", type(model).__name__, name |
| 1448 | + ) |
1463 | 1449 | return dumped |
1464 | 1450 |
|
1465 | | - @staticmethod |
1466 | | - def _dump_fallback_value(value: Any) -> Any: |
1467 | | - """Dump a field value JSON mode could not encode, keeping JSON mode for what inside it can be.""" |
1468 | | - if isinstance(value, BaseModel): |
1469 | | - return FunctionTool._dump_model(value) |
1470 | | - if isinstance(value, dict): |
1471 | | - dict_value = cast(dict[object, object], value) |
1472 | | - return {key: FunctionTool._dump_fallback_value(item) for key, item in dict_value.items()} |
1473 | | - if isinstance(value, (list, tuple, set, frozenset)): |
1474 | | - items = cast(Iterable[object], value) |
1475 | | - return [FunctionTool._dump_fallback_value(item) for item in items] |
1476 | | - try: |
1477 | | - return to_jsonable_python(value) |
1478 | | - except ValueError: |
1479 | | - # parse_result's json.dumps(default=str) turns it into a string |
1480 | | - return value |
1481 | | - |
1482 | 1451 | @staticmethod |
1483 | 1452 | def parse_result(result: Any) -> list[Content]: |
1484 | 1453 | """Convert a raw function return value to a list of Content items. |
|
0 commit comments