Skip to content

data

ChatMessage

Bases: NullableBaseModel

Model representing a chat message, contained within a request or response model under messages or choices field.

Attributes:

Name Type Description
role Literal['system', 'user', 'assistant', 'tool', 'function', 'developer']

The role of the message sender (user, assistant, etc.).

refusal str | None

The refusal message, if applicable.

tool_calls list[ToolCall] | None

A list of tool calls made during the message.

content str | list[dict[str, Any]] | None

The content of the message.

audio Audio | None

The audio data associated with the message.

annotations list[Annotation] | None

Any annotations associated with the message.

name str | None

The name of the message sender.

thinking str | None

The thinking process associated with the message.

split_thinking_from_response

split_thinking_from_response() -> Self

Splits the thinking process from the response content.

Returns:

Type Description
Self

A tuple containing the thinking process and the response content.

Source code in src/nexosapi/domain/data.py
@model_validator(mode="after")
def split_thinking_from_response(self) -> Self:
    """
    Splits the thinking process from the response content.

    :return: A tuple containing the thinking process and the response content.
    """
    if self.role == "assistant" and self.content:
        # First, check if content is a string and contains citations (they appear after the predefined text marker)
        if isinstance(self.content, str):
            citations = self.extract_citations(self.content)
            # If present, split the citations sections into individual list items (either starting from dash or a bullet point)
            if citations:
                for citation in citations:
                    if not self.annotations:
                        self.annotations = []

                    self.annotations.append(self.transform_citation_into_annotation(citation))
                    citation_text = f"[{citation['title']}]({citation['url']})"
                    self.content = self.content.replace(citation_text, "").strip()

            thinking = self.extract_thinking_section(self.content)
            self.content = self.content.replace("<think>", "").replace("</think>", "")
            if thinking:
                logging.info(f"[SDK] Thinking process detected for message from {self.name}")
                self.thinking = thinking.group(0)
                self.content = self.content.replace(thinking.group(1), "").strip()
    return self