Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: add fireworks provider (#9780)

Ari Webbf510f451144c
diff --git a/fern/openapi.json b/fern/openapi.json
index 572854d7a..9fbcf50dc 100644
--- a/fern/openapi.json
+++ b/fern/openapi.json
@@ -38683,6 +38683,7 @@
"deepseek",
"xai",
"zai",
+ "fireworks",
"openrouter",
"chatgpt_oauth"
],
@@ -43088,6 +43089,7 @@
"cerebras",
"chatgpt_oauth",
"deepseek",
+ "fireworks",
"google_ai",
"google_vertex",
"groq",
diff --git a/letta/adapters/letta_llm_stream_adapter.py b/letta/adapters/letta_llm_stream_adapter.py
index 440adb1d9..4636b1921 100644
--- a/letta/adapters/letta_llm_stream_adapter.py
+++ b/letta/adapters/letta_llm_stream_adapter.py
@@ -82,7 +82,7 @@ class LettaLLMStreamAdapter(LettaLLMAdapter):
run_id=self.run_id,
step_id=step_id,
)
- elif self.llm_config.model_endpoint_type in [ProviderType.openai, ProviderType.openrouter]:
+ elif self.llm_config.model_endpoint_type in [ProviderType.openai, ProviderType.openrouter, ProviderType.fireworks]:
# For non-v1 agents, always use Chat Completions streaming interface
self.interface = OpenAIStreamingInterface(
use_assistant_message=use_assistant_message,
diff --git a/letta/adapters/simple_llm_stream_adapter.py b/letta/adapters/simple_llm_stream_adapter.py
index a5d880d86..632a10e9b 100644
--- a/letta/adapters/simple_llm_stream_adapter.py
+++ b/letta/adapters/simple_llm_stream_adapter.py
@@ -89,6 +89,7 @@ class SimpleLLMStreamAdapter(LettaLLMStreamAdapter):
ProviderType.deepseek,
ProviderType.openrouter,
ProviderType.zai,
+ ProviderType.fireworks,
ProviderType.chatgpt_oauth,
]:
# Decide interface based on payload shape
diff --git a/letta/llm_api/fireworks_client.py b/letta/llm_api/fireworks_client.py
new file mode 100644
index 000000000..52e34a53e
--- /dev/null
+++ b/letta/llm_api/fireworks_client.py
@@ -0,0 +1,147 @@
+from typing import List, Optional
+
+from openai import AsyncOpenAI, AsyncStream, OpenAI
+from openai.types.chat.chat_completion import ChatCompletion
+from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
+
+from letta.helpers.json_helpers import sanitize_unicode_surrogates
+from letta.llm_api.openai_client import OpenAIClient
+from letta.llm_api.zai_client import is_zai_reasoning_model
+from letta.otel.tracing import trace_method
+from letta.schemas.enums import AgentType
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage
+from letta.schemas.openai.chat_completion_response import ChatCompletionResponse
+from letta.settings import model_settings
+
+
+class FireworksClient(OpenAIClient):
+ """Fireworks AI client — serves GLM-5 via OpenAI-compatible API."""
+
+ def requires_auto_tool_choice(self, llm_config: LLMConfig) -> bool:
+ return False
+
+ def supports_structured_output(self, llm_config: LLMConfig) -> bool:
+ return False
+
+ def is_reasoning_model(self, llm_config: LLMConfig) -> bool:
+ return is_zai_reasoning_model(llm_config.model)
+
+ @trace_method
+ def build_request_data(
+ self,
+ agent_type: AgentType,
+ messages: List[PydanticMessage],
+ llm_config: LLMConfig,
+ tools: Optional[List[dict]] = None,
+ force_tool_call: Optional[str] = None,
+ requires_subsequent_tool_call: bool = False,
+ tool_return_truncation_chars: Optional[int] = None,
+ ) -> dict:
+ data = super().build_request_data(agent_type, messages, llm_config, tools, force_tool_call, requires_subsequent_tool_call)
+
+ # Add thinking configuration for reasoning models
+ if self.is_reasoning_model(llm_config):
+ if llm_config.enable_reasoner:
+ data["extra_body"] = {
+ "thinking": {
+ "type": "enabled",
+ "clear_thinking": False,
+ }
+ }
+ else:
+ data["extra_body"] = {
+ "thinking": {
+ "type": "disabled",
+ }
+ }
+
+ # Fireworks (like Z.ai) uses max_tokens, not max_completion_tokens
+ if "max_completion_tokens" in data:
+ data["max_tokens"] = data.pop("max_completion_tokens")
+
+ # Sanitize empty text content — rejects empty text blocks
+ if "messages" in data:
+ for msg in data["messages"]:
+ content = msg.get("content") if isinstance(msg, dict) else getattr(msg, "content", None)
+ if isinstance(content, str) and not content.strip():
+ role = msg.get("role") if isinstance(msg, dict) else getattr(msg, "role", None)
+ has_tool_calls = msg.get("tool_calls") if isinstance(msg, dict) else getattr(msg, "tool_calls", None)
+ if role == "assistant" and has_tool_calls:
+ if isinstance(msg, dict):
+ msg["content"] = None
+ else:
+ msg.content = None
+ else:
+ if isinstance(msg, dict):
+ msg["content"] = "."
+ else:
+ msg.content = "."
+ elif isinstance(content, list):
+ for block in content:
+ if isinstance(block, dict) and block.get("type") == "text":
+ if not block.get("text", "").strip():
+ block["text"] = "."
+
+ # Fireworks uses strict validation (additionalProperties: false) and rejects
+ # reasoning fields that are not in their schema.
+ if "messages" in data:
+ for msg in data["messages"]:
+ for field in ("reasoning_content_signature", "redacted_reasoning_content", "omitted_reasoning_content"):
+ msg.pop(field, None)
+
+ return data
+
+ @trace_method
+ def request(self, request_data: dict, llm_config: LLMConfig) -> dict:
+ api_key = model_settings.fireworks_api_key
+ client = OpenAI(api_key=api_key, base_url=llm_config.model_endpoint)
+ response: ChatCompletion = client.chat.completions.create(**request_data)
+ return response.model_dump()
+
+ @trace_method
+ async def request_async(self, request_data: dict, llm_config: LLMConfig) -> dict:
+ request_data = sanitize_unicode_surrogates(request_data)
+ api_key = model_settings.fireworks_api_key
+ client = AsyncOpenAI(api_key=api_key, base_url=llm_config.model_endpoint)
+ response: ChatCompletion = await client.chat.completions.create(**request_data)
+ return response.model_dump()
+
+ @trace_method
+ async def stream_async(self, request_data: dict, llm_config: LLMConfig) -> AsyncStream[ChatCompletionChunk]:
+ request_data = sanitize_unicode_surrogates(request_data)
+ api_key = model_settings.fireworks_api_key
+ client = AsyncOpenAI(api_key=api_key, base_url=llm_config.model_endpoint)
+ response_stream: AsyncStream[ChatCompletionChunk] = await client.chat.completions.create(
+ **request_data, stream=True, stream_options={"include_usage": True}
+ )
+ return response_stream
+
+ @trace_method
+ async def convert_response_to_chat_completion(
+ self,
+ response_data: dict,
+ input_messages: List[PydanticMessage],
+ llm_config: LLMConfig,
+ ) -> ChatCompletionResponse:
+ chat_completion_response = await super().convert_response_to_chat_completion(response_data, input_messages, llm_config)
+
+ # Handle reasoning_content from response
+ if (
+ chat_completion_response.choices
+ and len(chat_completion_response.choices) > 0
+ and chat_completion_response.choices[0].message
+ and not chat_completion_response.choices[0].message.reasoning_content
+ ):
+ if "choices" in response_data and len(response_data["choices"]) > 0:
+ choice_data = response_data["choices"][0]
+ if "message" in choice_data and "reasoning_content" in choice_data["message"]:
+ reasoning_content = choice_data["message"]["reasoning_content"]
+ if reasoning_content:
+ chat_completion_response.choices[0].message.reasoning_content = reasoning_content
+ chat_completion_response.choices[0].message.reasoning_content_signature = None
+
+ if self.is_reasoning_model(llm_config) and llm_config.enable_reasoner:
+ chat_completion_response.choices[0].message.omitted_reasoning_content = True
+
+ return chat_completion_response
diff --git a/letta/llm_api/llm_client.py b/letta/llm_api/llm_client.py
index 181597430..713fb1bf6 100644
--- a/letta/llm_api/llm_client.py
+++ b/letta/llm_api/llm_client.py
@@ -115,6 +115,13 @@ class LLMClient:
put_inner_thoughts_first=put_inner_thoughts_first,
actor=actor,
)
+ case ProviderType.fireworks:
+ from letta.llm_api.fireworks_client import FireworksClient
+
+ return FireworksClient(
+ put_inner_thoughts_first=put_inner_thoughts_first,
+ actor=actor,
+ )
case ProviderType.chatgpt_oauth:
from letta.llm_api.chatgpt_oauth_client import ChatGPTOAuthClient
diff --git a/letta/llm_api/openai_client.py b/letta/llm_api/openai_client.py
index e615315ba..7ed209a89 100644
--- a/letta/llm_api/openai_client.py
+++ b/letta/llm_api/openai_client.py
@@ -641,14 +641,6 @@ class OpenAIClient(LLMClientBase):
tool.function.strict = False
request_data = data.model_dump(exclude_unset=True)
- # Fireworks uses strict validation (additionalProperties: false) and rejects
- # reasoning fields that are not in their schema.
- is_fireworks = llm_config.model_endpoint and "fireworks.ai" in llm_config.model_endpoint
- if is_fireworks and "messages" in request_data:
- for message in request_data["messages"]:
- for field in ("reasoning_content_signature", "redacted_reasoning_content", "omitted_reasoning_content"):
- message.pop(field, None)
-
# If Ollama
# if llm_config.handle.startswith("ollama/") and llm_config.enable_reasoner:
# Sadly, reasoning via the OpenAI proxy on Ollama only works for Harmony/gpt-oss
diff --git a/letta/schemas/enums.py b/letta/schemas/enums.py
index 3996a160e..a82e84047 100644
--- a/letta/schemas/enums.py
+++ b/letta/schemas/enums.py
@@ -57,6 +57,7 @@ class ProviderType(str, Enum):
cerebras = "cerebras"
chatgpt_oauth = "chatgpt_oauth"
deepseek = "deepseek"
+ fireworks = "fireworks"
google_ai = "google_ai"
google_vertex = "google_vertex"
groq = "groq"
diff --git a/letta/schemas/llm_config.py b/letta/schemas/llm_config.py
index 7d3e32a1b..8d9148139 100644
--- a/letta/schemas/llm_config.py
+++ b/letta/schemas/llm_config.py
@@ -51,6 +51,7 @@ class LLMConfig(BaseModel):
"deepseek",
"xai",
"zai",
+ "fireworks",
"openrouter",
"chatgpt_oauth",
] = Field(..., description="The endpoint type for the model.")
diff --git a/letta/services/llm_router/client.py b/letta/services/llm_router/client.py
index e7659f1aa..87fa858c3 100644
--- a/letta/services/llm_router/client.py
+++ b/letta/services/llm_router/client.py
@@ -22,7 +22,7 @@ if TYPE_CHECKING:
logger = get_logger(__name__)
# Auto mode model routing
-AUTO_MODE_PRIMARY = "zai/glm-5"
+AUTO_MODE_PRIMARY = "fireworks/glm-5"
AUTO_MODE_FALLBACK = "zai/glm-5"
# Circuit breaker configuration
@@ -36,6 +36,24 @@ CIRCUIT_STATE_KEY = "circuit:state:"
CIRCUIT_OPENED_AT_KEY = "circuit:opened:"
+def _build_fireworks_config() -> "LLMConfig":
+ """Build a hardcoded LLMConfig for Fireworks GLM-5 (not discoverable via provider sync)."""
+ from letta.schemas.enums import ProviderCategory
+ from letta.schemas.llm_config import LLMConfig
+ from letta.settings import model_settings
+
+ return LLMConfig(
+ model="accounts/fireworks/models/glm-5",
+ model_endpoint_type="fireworks",
+ model_endpoint=model_settings.fireworks_api_base,
+ context_window=180000,
+ max_tokens=16384,
+ handle="fireworks/glm-5",
+ provider_name="fireworks",
+ provider_category=ProviderCategory.base,
+ )
+
+
class CircuitState(str, Enum):
CLOSED = "CLOSED"
OPEN = "OPEN"
@@ -77,7 +95,7 @@ class LLMRoutingClient(LLMRoutingClientBase):
if auto_mode_enabled:
if await self._is_healthy(primary_handle):
- config = await provider_manager.get_llm_config_from_handle(primary_handle, actor)
+ config = _build_fireworks_config()
config = config.model_copy(
update={
"context_window": min(stored_llm_config.context_window, config.context_window),
diff --git a/letta/settings.py b/letta/settings.py
index 545bfa511..55f40d19e 100644
--- a/letta/settings.py
+++ b/letta/settings.py
@@ -150,6 +150,10 @@ class ModelSettings(BaseSettings):
# deepseek
deepseek_api_key: Optional[str] = None
+ # fireworks
+ fireworks_api_key: Optional[str] = None
+ fireworks_api_base: str = "https://api.fireworks.ai/inference/v1"
+
# xAI / Grok
xai_api_key: Optional[str] = None