Skip to main content

ResponseAdapter

Trait ResponseAdapter 

Source
pub trait ResponseAdapter {
    // Required methods
    fn __str__(&self) -> String;
    fn is_empty(&self) -> bool;
    fn to_bound_py_object<'py>(
        &self,
        py: Python<'py>,
    ) -> Result<Bound<'py, PyAny>, TypeError>;
    fn id(&self) -> &str;
    fn to_message_num(&self) -> Result<Vec<MessageNum>, TypeError>;
    fn usage<'py>(
        &self,
        py: Python<'py>,
    ) -> Result<Bound<'py, PyAny>, TypeError>;
    fn get_content(&self) -> ResponseContent;
    fn get_log_probs(&self) -> Vec<TokenLogProbs>;
    fn structured_output<'py>(
        &self,
        py: Python<'py>,
        output_type: Option<&Bound<'py, PyAny>>,
    ) -> Result<Bound<'py, PyAny>, TypeError>;
    fn structured_output_value(&self) -> Option<Value>;
    fn tool_call_output(&self) -> Option<Value>;
    fn response_text(&self) -> String;
}

Required Methods§

Source

fn __str__(&self) -> String

Returns a string representation of the response

Source

fn is_empty(&self) -> bool

Checks if the response is empty

Source

fn to_bound_py_object<'py>( &self, py: Python<'py>, ) -> Result<Bound<'py, PyAny>, TypeError>

Converts the response to a Python object

Source

fn id(&self) -> &str

Returns the response ID

Source

fn to_message_num(&self) -> Result<Vec<MessageNum>, TypeError>

Converts the response to a vector of MessageNum

Source

fn usage<'py>(&self, py: Python<'py>) -> Result<Bound<'py, PyAny>, TypeError>

Source

fn get_content(&self) -> ResponseContent

Retrieves the first content choice from the response

Source

fn get_log_probs(&self) -> Vec<TokenLogProbs>

Retrieves the log probabilities from the response

Source

fn structured_output<'py>( &self, py: Python<'py>, output_type: Option<&Bound<'py, PyAny>>, ) -> Result<Bound<'py, PyAny>, TypeError>

Returns the structured output of the response For all response types the flow is as follows:

  1. Check if the response has content (string/text)
  2. If no content, return Python None
  3. If content exists, check if an output_type/model is provided
  4. If output_type/model is provided, attempt to convert the content to that type
  5. If conversion fails, attempt to construct a generic Python object from the content
  6. If no output_type/model is provided, return the content as a generic Python object
§Arguments
  • py: The Python GIL token
  • output_type: An optional Python type/model to convert the content into. This can be a pydantic model or any object that implements model_validate_json that can parse from a JSON string.
§Returns
  • Result<Bound<'py, PyAny>, TypeError>: The structured output as a Python object or an error
Source

fn structured_output_value(&self) -> Option<Value>

Returns the structured output value as a serde_json::Value

Source

fn tool_call_output(&self) -> Option<Value>

Returns any tool calls made in the response, if applicable

Source

fn response_text(&self) -> String

Returns the output text of the response if available

Implementors§