Implementation:Langchain ai Langchain ChatFireworks
| Knowledge Sources | |
|---|---|
| Domains | LLM Framework, Fireworks AI, Chat Models, Tool Calling |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
The ChatFireworks class provides a LangChain chat model integration with the Fireworks AI API, supporting synchronous, asynchronous, and streaming chat completions with tool calling and structured output capabilities.
Description
This module implements ChatFireworks as a subclass of LangChain's BaseChatModel in the langchain-fireworks partner package. It wraps the Fireworks Python client library to provide an OpenAI-compatible chat completions interface. The class supports tool binding via bind_tools, structured output via with_structured_output (function calling, JSON schema, and JSON mode methods), streaming, and model profile auto-detection. The module also includes helper functions for converting between LangChain message types and the Fireworks API format.
Usage
Import ChatFireworks from langchain_fireworks and instantiate with a model name. Requires the FIREWORKS_API_KEY environment variable or explicit api_key parameter.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/fireworks/langchain_fireworks/chat_models.py - Lines: 1-1083
Signature
class ChatFireworks(BaseChatModel):
client: Any = Field(default=None, exclude=True)
async_client: Any = Field(default=None, exclude=True)
model_name: str = Field(alias="model")
temperature: float | None = None
stop: str | list[str] | None = Field(default=None, alias="stop_sequences")
model_kwargs: dict[str, Any] = Field(default_factory=dict)
fireworks_api_key: SecretStr = Field(alias="api_key", ...)
fireworks_api_base: str | None = Field(alias="base_url", ...)
request_timeout: float | tuple[float, float] | Any | None = Field(default=None, alias="timeout")
streaming: bool = False
n: int = 1
max_tokens: int | None = None
max_retries: int | None = None
Import
from langchain_fireworks import ChatFireworks
# or
from langchain_fireworks.chat_models import ChatFireworks
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model_name / model | str | Yes | Model name to use (e.g., "accounts/fireworks/models/gpt-oss-120b")
|
| temperature | float or None | No | Sampling temperature (default: None) |
| stop | str or list[str] or None | No | Default stop sequences. Alias: stop_sequences
|
| max_tokens | int or None | No | Maximum number of tokens to generate |
| fireworks_api_key | SecretStr | No | API key. Alias: api_key. Env: FIREWORKS_API_KEY
|
| fireworks_api_base | str or None | No | Base URL for API requests. Alias: base_url. Env: FIREWORKS_API_BASE
|
| request_timeout | float or tuple or None | No | Request timeout. Alias: timeout
|
| streaming | bool | No | Whether to stream results (default: False) |
| n | int | No | Number of completions to generate per prompt (default: 1) |
| max_retries | int or None | No | Maximum retries for failed requests |
| model_kwargs | dict | No | Additional parameters passed to the API create call
|
Outputs
| Name | Type | Description |
|---|---|---|
| ChatResult | ChatResult |
Contains ChatGeneration objects with AIMessage content, tool calls, and usage metadata
|
| ChatGenerationChunk | ChatGenerationChunk |
Streaming chunks containing AIMessageChunk with incremental content and tool call chunks
|
Key Mechanisms
Message Conversion
The module provides bidirectional conversion between LangChain messages and Fireworks API format:
_convert_message_to_dict: ConvertsBaseMessagesubtypes to API-compatible dictionaries, handling tool calls, function calls, and content normalization_convert_dict_to_message: Converts API response dictionaries back to LangChain message types with proper tool call parsing_convert_chunk_to_message_chunk: Converts streaming chunks toBaseMessageChunksubtypes with usage metadata
Tool Binding
The bind_tools method converts tool definitions to OpenAI format and supports multiple tool_choice modes:
- String names for specific tool selection
"auto","any","none"for automatic selection- Boolean
Truefor single-tool forced selection - Dictionary format for explicit tool specification
Structured Output
The with_structured_output method supports three methods:
function_calling: Uses tool-calling to enforce schema viaPydanticToolsParserorJsonOutputKeyToolsParserjson_schema: Uses Fireworks structured output withresponse_formatjson_mode: Uses Fireworks JSON mode with schema instructions in prompts
Model Profiles
The _set_model_profile validator auto-loads model capability profiles from langchain_fireworks.data._profiles to configure features like token limits and supported modalities.
Usage Examples
Basic Usage
from langchain_fireworks import ChatFireworks
model = ChatFireworks(
model="accounts/fireworks/models/gpt-oss-120b",
temperature=0,
)
response = model.invoke("What is the capital of France?")
print(response.content)
Tool Calling
from pydantic import BaseModel, Field
from langchain_fireworks import ChatFireworks
class GetWeather(BaseModel):
"""Get the current weather in a given location."""
location: str = Field(description="City and state, e.g. San Francisco, CA")
model = ChatFireworks(model="accounts/fireworks/models/gpt-oss-120b")
model_with_tools = model.bind_tools([GetWeather])
response = model_with_tools.invoke("What's the weather in NYC?")
print(response.tool_calls)
Structured Output
from pydantic import BaseModel
from langchain_fireworks import ChatFireworks
class Answer(BaseModel):
answer: str
justification: str
model = ChatFireworks(model="accounts/fireworks/models/gpt-oss-120b", temperature=0)
structured = model.with_structured_output(Answer)
result = structured.invoke("What weighs more, a pound of bricks or feathers?")
print(result)