Anthropic adapter.
This adapter is implemented using the Messages API and structured outputs. For older models that don't support structured outputs, function calling is used.
Params
params argument should be shaped as a Create message request body.
Example
InstructorLite.instruct(%{
messages: [%{role: "user", content: "John is 25yo"}],
model: "claude-sonnet-4-5",
metadata: %{user_id: "3125"}
},
response_model: %{name: :string, age: :integer},
adapter: InstructorLite.Adapters.Anthropic,
adapter_context: [api_key: Application.fetch_env!(:instructor_lite, :anthropic_key)]
)
{:ok, %{name: "John", age: 25}}
Summary
Functions
Parse API response in search of plain text output.
Updates params with prompt based on json_schema and notes.
Parse API response.
Updates params with prompt for retrying a request.
Make request to Anthropic API
Functions
Parse API response in search of plain text output.
Can return:
{:ok, text_output}on success.{:error, :unexpected_response, response}if response is of unexpected shape.
Updates params with prompt based on json_schema and notes.
Also specifies default claude-haiku-4-5 model and 1024 max_tokens if not provided by a user.
Parse API response.
Can return:
{:ok, parsed_object}on success.{:error, :unexpected_response, response}if response is of unexpected shape.
Updates params with prompt for retrying a request.
The error is represented as a user message or erroneous tool_result for
older models.
Make request to Anthropic API
Options
:api_key(String.t/0) - Required. Anthropic API key:http_client(atom/0) - Any module that followsReq.post/2interface The default value isReq.:http_options(keyword/0) - Options passed tohttp_client.post/2The default value is[receive_timeout: 60000].:url(String.t/0) - API endpoint to use for sending requests The default value is"https://api.anthropic.com/v1/messages".:version(String.t/0) - Anthropic API version The default value is"2023-06-01".