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
nameis a string (defaults to"unknown_tool"if missing) - Ensures
argsis a dict (parses JSON if string, else wraps as{"value": ...}) - Always sets
typeto"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_idis a non-empty string (generates UUID if missing/empty) - Always ensures
nameis a string (fallback to 'unknown_tool') - Always ensures
argsis a dict (tries to parse string as JSON, else wraps as {'value': ...}) - Always sets
typeto '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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_meta-0.4.1.tar.gz.
File metadata
- Download URL: langchain_meta-0.4.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66a0250e3431a295ece5aad0574fa01d69ea61c745cb9f44e2b8fa80d168d3cc
|
|
| MD5 |
c38865e0a97d8add407175dd2181c8dd
|
|
| BLAKE2b-256 |
5fd92b636b2c3c9a2ac76a1e4ff7c3742fa4460e46f9e998fb862e211b251532
|
Provenance
The following attestation bundles were made for langchain_meta-0.4.1.tar.gz:
Publisher:
python-publish.yml on mattpetters/langchain-meta
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_meta-0.4.1.tar.gz -
Subject digest:
66a0250e3431a295ece5aad0574fa01d69ea61c745cb9f44e2b8fa80d168d3cc - Sigstore transparency entry: 213998304
- Sigstore integration time:
-
Permalink:
mattpetters/langchain-meta@7ad2b230a11d696bae7fbfe42b55412daf2b9d8e -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/mattpetters
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7ad2b230a11d696bae7fbfe42b55412daf2b9d8e -
Trigger Event:
release
-
Statement type:
File details
Details for the file langchain_meta-0.4.1-py3-none-any.whl.
File metadata
- Download URL: langchain_meta-0.4.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc51aadb1a314c1b70f05f5c966600687a352a3e4ae26a66a24e0f467f6be375
|
|
| MD5 |
5ef8a803066c0ecf58f93fcaddf9dbe1
|
|
| BLAKE2b-256 |
5687407229e17874caa4f6c3ab91f03b2e0b1861dda933f8332c8a696bb83292
|
Provenance
The following attestation bundles were made for langchain_meta-0.4.1-py3-none-any.whl:
Publisher:
python-publish.yml on mattpetters/langchain-meta
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_meta-0.4.1-py3-none-any.whl -
Subject digest:
cc51aadb1a314c1b70f05f5c966600687a352a3e4ae26a66a24e0f467f6be375 - Sigstore transparency entry: 213998306
- Sigstore integration time:
-
Permalink:
mattpetters/langchain-meta@7ad2b230a11d696bae7fbfe42b55412daf2b9d8e -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/mattpetters
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7ad2b230a11d696bae7fbfe42b55412daf2b9d8e -
Trigger Event:
release
-
Statement type: