Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
LlamaAPIClient
✓ import LlamaAPIClient from 'llama-api-client';
✗ const LlamaAPIClient = require('llama-api-client');
This is the default export for the main client class. While CommonJS `require` might work in some transpiled environments, native ESM `import` is the idiomatic and recommended way, especially in modern TypeScript projects.
toFile
✓ import { toFile } from 'llama-api-client';
✗ import LlamaAPIClient from 'llama-api-client';
const file = LlamaAPIClient.toFile(buffer, 'name.txt');
The `toFile` utility function, used for standardizing file upload inputs, is a named export, not a property of the default `LlamaAPIClient` instance or class.
Chat.CompletionCreateParams
✓ import LlamaAPIClient from 'llama-api-client';
const params: LlamaAPIClient.Chat.CompletionCreateParams = {
messages: [{ content: 'string', role: 'user' }],
model: 'model',
};
Type definitions for request parameters are nested as static members under the main `LlamaAPIClient` object for clear categorization and IntelliSense.
APIError
✓ import LlamaAPIClient from 'llama-api-client';
try { /* ... */ } catch (error) {
if (error instanceof LlamaAPIClient.APIError) { /* ... */ }
}
The base class for all API-specific errors thrown by the client is nested under the main `LlamaAPIClient` object, allowing for structured error handling.
This quickstart demonstrates how to initialize the Llama API client, perform a basic chat completion, handle streaming responses, and shows how to approach error handling using `LlamaAPIClient.APIError`.
import LlamaAPIClient from 'llama-api-client';
import { toFile } from 'llama-api-client'; // Included for file upload example context, though commented out for brevity
// Ensure your LLAMA_API_KEY is set as an environment variable (e.g., in a .env file or production config).
// For local development, 'dotenv' package might be used: `require('dotenv').config();`
const client = new LlamaAPIClient({
apiKey: process.env['LLAMA_API_KEY'] ?? '', // Provide an empty string fallback or handle validation for missing key
});
async function runLlamaClientExamples() {
try {
console.log('--- Creating a Chat Completion ---');
const chatResponse = await client.chat.completions.create({
messages: [{ content: 'Hello, what is the capital of France?', role: 'user' }],
model: 'llama-3-8b-instruct', // Using an example model identifier
max_tokens: 50,
temperature: 0.7,
});
console.log('Chat completion response:', chatResponse.completion_message?.content);
console.log('\n--- Streaming Response Example ---');
const stream = await client.chat.completions.create({
messages: [{ content: 'Tell me a short story about a brave knight.', role: 'user' }],
model: 'llama-3-8b-instruct',
stream: true,
max_tokens: 100,
});
process.stdout.write('Streamed story: ');
for await (const chunk of stream) {
if (chunk.completion_message) {
process.stdout.write(chunk.completion_message.content || '');
}
}
process.stdout.write('\n'); // Newline after stream finishes
// Conceptual example for file upload (requires an 'uploads' endpoint and actual file data)
// For a real scenario, you would typically use fs.createReadStream, a web File object, or Buffer.
// const dummyFileContent = Buffer.from('This is a test file for upload.');
// const dummyFile = await toFile(dummyFileContent, 'my-document.txt');
// const uploadResponse = await client.uploads.create({ file: dummyFile, purpose: 'fine-tune' });
// console.log('\nUpload initiated:', uploadResponse);
} catch (error) {
if (error instanceof LlamaAPIClient.APIError) {
console.error('Llama API Error caught:', error.status, error.code, error.message, 'Details:', error.error);
} else {
console.error('An unexpected error occurred:', error);
}
}
}
runLlamaClientExamples();
Errors
Common errors & fixes
Error: LLAMA_API_KEY is not set
The Llama API client was initialized without an API key, and the `LLAMA_API_KEY` environment variable was not found in the current process environment.
fixSet the `LLAMA_API_KEY` environment variable in your system or shell, or explicitly pass the `apiKey` option to the `LlamaAPIClient` constructor: `new LlamaAPIClient({ apiKey: 'your_key' })`. LlamaAPIClient.APIError: Request failed with status code 401
The provided API key is either invalid, expired, revoked, or does not have the necessary permissions for the requested operation.
fixVerify that your `LLAMA_API_KEY` is correct, active, and has appropriate access rights by checking your Llama API account and key management page.
LlamaAPIClient.APIError: Request failed with status code 400 (Bad Request)
The request payload or parameters sent to the API were malformed, contained invalid values, or were incomplete according to the Llama API's specification.
fixReview the request parameters you are sending to the API call against the official Llama API documentation and the TypeScript type definitions provided by the library for correctness.
TypeError: fetch failed
A low-level network connectivity issue (e.g., DNS resolution failure, firewall block, or no internet connection) prevented the client from establishing a connection to the Llama API endpoint.
fixCheck your internet connection, ensure the Llama API service is operational (consult their status page), and verify that your network environment allows outgoing HTTPS connections to the API endpoint.
Audit
Dependencies
No dependency data recorded yet.