Skip to content

Commit e2fb636

Browse files
authored
Fix inference failure caused by improper handling of large chat templates (#1226)
2 parents 39759b9 + 6e65abd commit e2fb636

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

ads/aqua/extension/model_handler.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# Copyright (c) 2024, 2025 Oracle and/or its affiliates.
33
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
4-
4+
import json
55
from typing import Optional
66
from urllib.parse import urlparse
77

@@ -341,9 +341,13 @@ def get(self, model_id):
341341
):
342342
try:
343343
oci_data_science_model = OCIDataScienceModel.from_id(model_id)
344+
chat_template = oci_data_science_model.get_custom_metadata_artifact("chat_template")
345+
chat_template = chat_template.decode("utf-8")
346+
347+
return self.finish(json.dumps({"chat_template": chat_template}))
348+
344349
except Exception as e:
345-
raise HTTPError(404, f"Model not found for id: {model_id}. Details: {str(e)}")
346-
return self.finish(oci_data_science_model.get_custom_metadata_artifact("chat_template"))
350+
raise HTTPError(404, f"Failed to fetch chat template for model_id={model_id}. Details: {str(e)}")
347351

348352
raise HTTPError(400, f"The request {self.request.path} is invalid.")
349353

tests/unitary/with_extras/aqua/test_model_handler.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*--
33
# Copyright (c) 2024, 2025 Oracle and/or its affiliates.
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5+
import json
56
from unicodedata import category
67
from unittest import TestCase
78
from unittest.mock import MagicMock, patch, ANY
@@ -272,11 +273,13 @@ def test_get_valid_path(self, mock_urlparse, mock_from_id):
272273
mock_urlparse.return_value = request_path
273274

274275
model_mock = MagicMock()
275-
model_mock.get_custom_metadata_artifact.return_value = "chat_template_string"
276+
model_mock.get_custom_metadata_artifact.return_value = b"chat_template_string"
276277
mock_from_id.return_value = model_mock
277278

278279
self.model_chat_template_handler.get(model_id="test_model_id")
279-
self.model_chat_template_handler.finish.assert_called_with("chat_template_string")
280+
self.model_chat_template_handler.finish.assert_called_with(
281+
json.dumps({"chat_template": "chat_template_string"})
282+
)
280283
model_mock.get_custom_metadata_artifact.assert_called_with("chat_template")
281284

282285
@patch("ads.aqua.extension.model_handler.urlparse")
@@ -361,7 +364,7 @@ def test_post_model_not_found(self, mock_write_error, mock_from_id):
361364
_, exc_instance, _ = exc_info
362365
assert isinstance(exc_instance, HTTPError)
363366
assert exc_instance.status_code == 404
364-
assert "Model not found" in str(exc_instance)
367+
assert "Model not found for id" in str(exc_instance)
365368

366369

367370
class TestAquaHuggingFaceHandler:

0 commit comments

Comments
 (0)