Skip to content

Commit 67afb60

Browse files
[OPIK-5877] create_chat_prompt uses authenticated client instead of global client (#6258)
* [OPIK-5877] fix chat prompt creation * [OPIK-5877] apply validation * [OPIK-5877] fix tests * Trigger pipeline re-run * Trigger pipeline re-run * [OPIK-5877] move import
1 parent 2445dd7 commit 67afb60

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

sdks/python/src/opik/api_objects/opik_client.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import contextvars
33
import datetime
44
import functools
5+
import json
56
import logging
67
from typing import (
78
Any,
@@ -45,6 +46,7 @@
4546
from .experiment import rest_operations as experiment_rest_operations
4647
from . import prompt as prompt_module
4748
from .prompt import client as prompt_client
49+
from ..validation.chat_prompt_messages import ChatPromptMessagesValidator
4850
from .agent_config.base import Config
4951
from .agent_config.config import ConfigManager
5052
from .threads import threads_client
@@ -2069,18 +2071,28 @@ def create_chat_prompt(
20692071
PromptTemplateStructureMismatch: If a text prompt with the same name already exists (template structure is immutable).
20702072
ApiError: If there is an error during the creation of the prompt.
20712073
"""
2074+
validator = ChatPromptMessagesValidator(messages)
2075+
validator.validate()
2076+
validator.raise_if_validation_failed()
2077+
2078+
prompt_client_ = prompt_client.PromptClient(self._rest_client)
20722079
project_name = self._resolve_project_name(project_name)
2073-
return prompt_module.ChatPrompt(
2080+
messages_str = json.dumps(messages)
2081+
prompt_version = prompt_client_.create_prompt(
20742082
name=name,
2075-
messages=messages,
2083+
prompt=messages_str,
20762084
metadata=metadata,
20772085
type=type,
2086+
template_structure="chat",
20782087
id=id,
20792088
description=description,
20802089
change_description=change_description,
20812090
tags=tags,
20822091
project_name=project_name,
20832092
)
2093+
return prompt_module.ChatPrompt.from_fern_prompt_version(
2094+
name, prompt_version, project_name=project_name
2095+
)
20842096

20852097
def get_prompt(
20862098
self,

sdks/python/tests/unit/api_objects/test_opik_client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,13 @@ class TestOpikClientCreateChatPrompt:
956956
def setup(self):
957957
self.opik_client_ = opik_client.Opik(project_name="default-project")
958958
self.messages = [{"role": "user", "content": "Hello"}]
959+
self.mock_version = _make_chat_prompt_version()
959960

960-
with patch.object(prompt_module.ChatPrompt, "sync_with_backend"):
961+
with patch.object(
962+
prompt_client_module.PromptClient,
963+
"create_prompt",
964+
return_value=self.mock_version,
965+
) as self.mock_create:
961966
yield
962967

963968
def test_create_chat_prompt__no_project_name__uses_default_project(self):

0 commit comments

Comments
 (0)