From 694adb150116b8ebb5075eeb2fc0107fe6daf7c6 Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Mon, 30 Dec 2024 13:57:41 -0800 Subject: [PATCH] [bugfix] fix broken vision inference, change serialization for bytes (#693) # What does this PR do? - vision inference via image as binary bytes fails with serialization error - add custom serialization for "bytes" in `_URLOrData` ## Test Plan ``` pytest -v -s -k "fireworks" --inference-model="meta-llama/Llama-3.2-11B-Vision-Instruct" ./llama_stack/providers/tests/inference/test_vision_inference.py::TestVisionModelInference::test_vision_chat_completion_non_streaming ``` **Before** image **After** image image ## Sources Please link relevant resources if necessary. ## Before submitting - [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case). - [ ] Ran pre-commit to handle lint / formatting issues. - [ ] Read the [contributor guideline](https://github.com/meta-llama/llama-stack/blob/main/CONTRIBUTING.md), Pull Request section? - [ ] Updated relevant documentation. - [ ] Wrote necessary unit or integration tests. --- llama_stack/apis/common/content_types.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/llama_stack/apis/common/content_types.py b/llama_stack/apis/common/content_types.py index 121218a29..629e0e94d 100644 --- a/llama_stack/apis/common/content_types.py +++ b/llama_stack/apis/common/content_types.py @@ -4,11 +4,12 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. +import base64 from typing import Annotated, List, Literal, Optional, Union from llama_models.schema_utils import json_schema_type, register_schema -from pydantic import BaseModel, Field, model_validator +from pydantic import BaseModel, Field, field_serializer, model_validator @json_schema_type @@ -27,6 +28,12 @@ class _URLOrData(BaseModel): return values return {"url": values} + @field_serializer("data") + def serialize_data(self, data: Optional[bytes], _info): + if data is None: + return None + return base64.b64encode(data).decode("utf-8") + @json_schema_type class ImageContentItem(_URLOrData):