Skip to main content

An integration wrapper package connecting Meta's Llama API client and LangGraph. Implements ChatModel interface from LangChain.

Project description

langchain-meta

Native integration between the Meta Llama API 🦙 and the LangChain/LangGraph ecosystem, ⛓ providing fast hosted access to Meta's powerful Llama 4 models to power your Langgraph agents. Fully implements ChatModel interface.

Installation

pip install langchain-meta

Set up your credentials with environment variables:

export META_API_KEY="your-api-key"
export META_API_BASE_URL="https://api.llama.com/v1"
export META_MODEL_NAME="Llama-4-Maverick-17B-128E-Instruct-FP8"
# Optional, see list: https://llama.developer.meta.com/docs/api/models/

Usage

ChatMetaLlama

from langchain_meta import ChatMetaLlama

# Initialize with API key and base URL
llm = ChatMetaLlama(
    model="Llama-4-Maverick-17B-128E-Instruct-FP8",
    api_key="your-meta-api-key",
    base_url="https://api.llama.com/v1/"
)

# Basic invocation
from langchain_core.messages import HumanMessage
response = llm.invoke([HumanMessage(content="Hello Llama!")])
print(response.content)

LangSmith Integration

ChatMetaLlama is fully compatible with LangSmith, providing comprehensive tracing and observability for your Meta LLM applications.

Key features of LangSmith integration:

  • Token Usage Tracking: Get accurate input/output token counts for cost estimation
  • Request/Response Logging: View full context of all prompts and completions
  • Tool Execution Tracing: Monitor tool calls and their execution
  • Runtime Metrics: Track latency and other performance metrics

To enable LangSmith tracing, set these environment variables:

export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY="your-api-key"
export LANGSMITH_PROJECT="your-project-name"

See the examples/langsmith_integration.py script for a complete example of LangSmith integration.

Utility Functions

meta_agent_factory

A utility to create LangChain runnables with Meta-specific configurations. Handles structured output and ensures streaming is disabled when needed for Meta API compatibility.

from langchain_meta import meta_agent_factory, ChatMetaLlama
from langchain_core.tools import Tool
from pydantic import BaseModel

# Create LLM
llm = ChatMetaLlama(api_key="your-meta-api-key")

# Example with tools
tools = [Tool.from_function(func=lambda x: x, name="example", description="Example tool")]
agent = meta_agent_factory(
    llm=llm,
    tools=tools,
    system_prompt_text="You are a helpful assistant that uses tools.",
    disable_streaming=True
)

# Example with structured output
class ResponseSchema(BaseModel):
    answer: str
    confidence: float

structured_agent = meta_agent_factory(
    llm=llm,
    output_schema=ResponseSchema,
    system_prompt_text="Return structured answers with confidence scores."
)

extract_json_response

A robust utility to extract JSON from various response formats, handling direct JSON objects, code blocks with backticks, or JSON-like patterns in text.

from langchain_meta import extract_json_response

# Parse various response formats
result = llm.invoke("Return a JSON with name and age")
parsed_json = extract_json_response(result.content)

Key Features

  • Direct Native API Access: Connect to Meta Llama models through their official API for full feature compatibility
  • Seamless Tool Calling: Intelligent conversion between LangChain tool formats and Llama API requirements
  • Complete Message History Support: Proper conversion of all LangChain message types
  • Multi-Agent System Compatibility: Drop-in replacement for ChatOpenAI in LangGraph workflows
  • LangSmith Integration: Full observability and tracing for debugging and monitoring

Chat Models

from langchain_meta import ChatMetaLlama

llm = ChatMetaLlama()
llm.invoke("Who directed the movie The Social Network?")

LangGraph & Multi-Agent Integration

The ChatMetaLlama class works with LangGraph nodes and complex agent systems:

from langchain_meta import ChatMetaLlama
from langchain_core.tools import tool

# Works with @tool decorations
@tool
def get_weather(location: str) -> str:
    """Get the current weather for a location."""
    return f"The weather in {location} is sunny."

# Create LLM with tools
llm = ChatMetaLlama(model="Llama-4-Maverick-17B-128E-Instruct-FP8")
llm_with_tools = llm.bind_tools([get_weather])

# Works in agent nodes and graph topologies
response = llm_with_tools.invoke("What's the weather in Seattle?")

Advanced Features

  • Streaming Support: Streaming implementation for both content and tool calls
  • Context Preservation: Correctly handles the full conversation context in agent graphs
  • Error Resilience: Robust handling of tool call parsing errors and response validation
  • Format Compatibility: Support for structured output Pydantic objects
  • Observability: Complete LangSmith integration for tracing and debugging

Robust Tool Call Normalization

This module automatically normalizes all tool calls from Llama/Meta API:

  • Ensures every tool call has a valid string id (generates one if missing/empty)
  • Ensures name is a string (defaults to "unknown_tool" if missing)
  • Ensures args is a dict (parses JSON if string, else wraps as {"value": ...})
  • Always sets type to "function"
  • Logs a warning for any repair

Best Practices:

  • Define your tool schemas as simply as possible (avoid advanced JSON Schema features).
  • Provide clear prompt examples to the LLM for tool calling.
  • Be aware of backend limitations (see vLLM Issue #15236).

Your code is robust to malformed tool calls, but clear schemas and prompts will maximize reliability.

Defensive Tool Call Handling

LangChain Meta includes robust defensive mechanisms for handling malformed tool calls:

  • Always ensures tool_call_id is a non-empty string (generates UUID if missing/empty)
  • Always ensures name is a string (fallback to 'unknown_tool')
  • Always ensures args is a dict (tries to parse string as JSON, else wraps as {'value': ...})
  • Always sets type to 'function'
  • Logs warnings for any repairs made

This makes the integration more resilient when working with models that may sometimes produce malformed tool calls.

Contributing

We welcome contributions! Please see the CONTRIBUTING.md file for details.

License

This project is licensed under the MIT License.

Llama 4, Llama AI API, etc trademarks belong to their respective owners (Meta) I just made this to make my life easier and thought I'd share. 😊

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_meta-0.4.0.tar.gz (46.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_meta-0.4.0-py3-none-any.whl (48.6 kB view details)

Uploaded Python 3

File details

Details for the file langchain_meta-0.4.0.tar.gz.

File metadata

  • Download URL: langchain_meta-0.4.0.tar.gz
  • Upload date:
  • Size: 46.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for langchain_meta-0.4.0.tar.gz
Algorithm Hash digest
SHA256 df2e87e43eb022d5f987b60cdf029280b32ddbffd93e5da69f9c888699702124
MD5 abe84438bb31b72ff8b519030293dec8
BLAKE2b-256 1d0d4ee12d7676ecfe4ff0a8ab7a13b5d59859dbbb6ad62311b87f3a5827db00

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_meta-0.4.0.tar.gz:

Publisher: python-publish.yml on mattpetters/langchain-meta

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_meta-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: langchain_meta-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 48.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for langchain_meta-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2bde364b4894dc8b545dfa3bf7769a51c5a81b6954dd5723d84eea91f0d4deae
MD5 57dea307774bb530c2785c8dd3b54e80
BLAKE2b-256 ae1b52f4a1ebfb3add772e8514401f1b37fbebdd014ef4db25c7d741d3ac16b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_meta-0.4.0-py3-none-any.whl:

Publisher: python-publish.yml on mattpetters/langchain-meta

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page