Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Helicone Helicone GetLLMSchemaResponse

From Leeroopedia
Knowledge Sources
Domains LLM Observability, Response Normalization, Anthropic
Last Updated 2026-02-14 00:00 GMT

Overview

Concrete function for parsing Anthropic-format LLM responses (including AWS Bedrock variant) into the normalized LlmSchema["response"] structure, provided by the llm-mapper package.

Description

getLLMSchemaResponse accepts a raw Anthropic response object and produces a normalized response containing a messages array of Message objects and optionally a model string. It handles four distinct response formats: error responses, AWS Bedrock Anthropic responses, Claude 3 Messages API responses, and legacy choices-based responses. Each content block (text, tool_use, or other) is converted via the internal anthropicContentToMessage helper. String content is cleaned of "undefined" artifacts and trimmed.

Usage

Called internally by the mapAnthropicRequest mapper to parse the response portion of an Anthropic LLM interaction. Also usable standalone when you need to parse an Anthropic response body in isolation.

Code Reference

Source Location

  • Repository: Helicone
  • File: packages/llm-mapper/mappers/anthropic/responseParser.ts

Signature

export const getLLMSchemaResponse = (
  response: any
): LlmSchema["response"]

Import

import { getLLMSchemaResponse } from "@helicone-package/llm-mapper/mappers/anthropic/responseParser";

I/O Contract

Inputs

Name Type Required Description
response any Yes Raw Anthropic response object. May be in one of four formats: (1) error response with error.heliconeMessage or error object, (2) AWS Bedrock format with output.message.content, (3) Claude 3 Messages format with type: "message" and content array, or (4) legacy format with choices[] array.

Outputs

Name Type Description
(return) LlmSchema["response"] Normalized response object containing optional messages: Message[], optional model: string, and optional error: { heliconeMessage: any }

Format Detection Logic

The function processes responses in the following priority order:

1. Error Responses

If the response contains an error property, it returns immediately with:

{
  error: {
    heliconeMessage: response.error.heliconeMessage ?? JSON.stringify(response.error, null, 2)
  }
}

2. AWS Bedrock Anthropic Format

Detected by response.output.message.content. The content may be an array or a single object:

// Input shape:
{
  output: {
    message: {
      role: "assistant",
      content: [{ type: "text", text: "..." }, { type: "tool_use", name: "...", input: {...} }]
    }
  }
}

3. Claude 3 Messages API Format

Detected by response.type === "message" || "message_stop" and response.content:

// Input shape:
{
  type: "message",
  role: "assistant",
  content: [{ type: "text", text: "..." }],
  model: "claude-3-opus-20240229"
}

4. Legacy Choices Format

Detected by response.choices being an array. Handles nested content arrays, JSON-encoded content strings, and plain text content:

// Input shape:
{
  choices: [{
    message: {
      role: "assistant",
      content: "..." | [{ type: "text", text: "..." }] | '["json-encoded"]'
    }
  }]
}

Content Block Conversion

The internal anthropicContentToMessage helper converts individual content blocks:

Content Type Output Message _type Extracted Fields
{ type: "text", text: "..." } "message" content from text field, cleaned and trimmed
{ type: "tool_use", name: "fn", input: {...} } "functionCall" tool_calls[0].name and tool_calls[0].arguments extracted from name/input
String "message" Content is the string itself
Array "message" Content is array items joined with text extraction
Other object "message" Content is JSON.stringify(content, null, 2)

Usage Examples

Basic Usage

import { getLLMSchemaResponse } from "@helicone-package/llm-mapper/mappers/anthropic/responseParser";

// Claude 3 Messages API response
const response = {
  type: "message",
  role: "assistant",
  content: [
    { type: "text", text: "Hello! How can I help you today?" }
  ],
  model: "claude-3-opus-20240229",
  usage: { input_tokens: 10, output_tokens: 15 }
};

const parsed = getLLMSchemaResponse(response);
// Returns:
// {
//   messages: [{
//     id: "random-id",
//     content: "Hello! How can I help you today?",
//     _type: "message",
//     role: "assistant"
//   }],
//   model: "claude-3-opus-20240229"
// }

Tool Use Response

const toolResponse = {
  type: "message",
  role: "assistant",
  content: [
    { type: "text", text: "Let me look that up." },
    { type: "tool_use", id: "call_123", name: "search", input: { query: "weather" } }
  ],
  model: "claude-3-sonnet-20240229"
};

const parsed = getLLMSchemaResponse(toolResponse);
// parsed.messages[0] -> { _type: "message", content: "Let me look that up.", role: "assistant" }
// parsed.messages[1] -> { _type: "functionCall", content: "", role: "assistant",
//                         tool_calls: [{ name: "search", arguments: { query: "weather" } }] }

Bedrock Response

const bedrockResponse = {
  output: {
    message: {
      role: "assistant",
      content: [{ type: "text", text: "Response from Bedrock." }]
    }
  },
  model: "anthropic.claude-3-sonnet-20240229-v1:0"
};

const parsed = getLLMSchemaResponse(bedrockResponse);
// Correctly extracts from the nested output.message.content path

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment