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: GPT-5 support (#3924)

Kevin Lina22d6b131550
diff --git a/letta/llm_api/openai_client.py b/letta/llm_api/openai_client.py
index c79867203..de1320023 100644
--- a/letta/llm_api/openai_client.py
+++ b/letta/llm_api/openai_client.py
@@ -53,6 +53,11 @@ def is_openai_5_model(model: str) -> bool:
return model.startswith("gpt-5")
+def supports_verbosity_control(model: str) -> bool:
+ """Check if the model supports verbosity control, currently only GPT-5 models support this"""
+ return is_openai_5_model(model)
+
+
def accepts_developer_role(model: str) -> bool:
"""Checks if the model accepts the 'developer' role. Note that not all reasoning models accept this role.
@@ -207,6 +212,10 @@ class OpenAIClient(LLMClientBase):
temperature=llm_config.temperature if supports_temperature_param(model) else 1.0,
)
+ # Add verbosity control for GPT-5 models
+ if supports_verbosity_control(model) and llm_config.verbosity:
+ data.verbosity = llm_config.verbosity
+
if llm_config.frequency_penalty is not None:
data.frequency_penalty = llm_config.frequency_penalty
@@ -235,8 +244,8 @@ class OpenAIClient(LLMClientBase):
tool.function = FunctionSchema(**structured_output_version)
except ValueError as e:
logger.warning(f"Failed to convert tool function to structured output, tool={tool}, error={e}")
-
- return data.model_dump(exclude_unset=True)
+ request_data = data.model_dump(exclude_unset=True)
+ return request_data
@trace_method
def request(self, request_data: dict, llm_config: LLMConfig) -> dict:
@@ -244,7 +253,6 @@ class OpenAIClient(LLMClientBase):
Performs underlying synchronous request to OpenAI API and returns raw response dict.
"""
client = OpenAI(**self._prepare_client_kwargs(llm_config))
-
response: ChatCompletion = client.chat.completions.create(**request_data)
return response.model_dump()
@@ -255,7 +263,6 @@ class OpenAIClient(LLMClientBase):
"""
kwargs = await self._prepare_client_kwargs_async(llm_config)
client = AsyncOpenAI(**kwargs)
-
response: ChatCompletion = await client.chat.completions.create(**request_data)
return response.model_dump()
diff --git a/letta/schemas/llm_config.py b/letta/schemas/llm_config.py
index f9ccc3580..c162317a8 100644
--- a/letta/schemas/llm_config.py
+++ b/letta/schemas/llm_config.py
@@ -70,10 +70,50 @@ class LLMConfig(BaseModel):
description="Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. From OpenAI: Number between -2.0 and 2.0.",
)
compatibility_type: Optional[Literal["gguf", "mlx"]] = Field(None, description="The framework compatibility type for the model.")
+ verbosity: Optional[Literal["low", "medium", "high"]] = Field(
+ "medium",
+ description="Soft control for how verbose model output should be, used for GPT-5 models.",
+ )
# FIXME hack to silence pydantic protected namespace warning
model_config = ConfigDict(protected_namespaces=())
+ @model_validator(mode="before")
+ @classmethod
+ def set_model_specific_defaults(cls, values):
+ """
+ Set model-specific default values for fields like max_tokens, context_window, etc.
+ This ensures the same defaults from default_config are applied automatically.
+ """
+ model = values.get("model")
+ if model is None:
+ return values
+
+ # Set max_tokens defaults based on model
+ if values.get("max_tokens") is None:
+ if model == "gpt-5":
+ values["max_tokens"] = 16384
+ elif model == "gpt-4.1":
+ values["max_tokens"] = 8192
+ # For other models, the field default of 4096 will be used
+
+ # Set context_window defaults if not provided
+ if values.get("context_window") is None:
+ if model == "gpt-5":
+ values["context_window"] = 128000
+ elif model == "gpt-4.1":
+ values["context_window"] = 256000
+ elif model == "gpt-4o" or model == "gpt-4o-mini":
+ values["context_window"] = 128000
+ elif model == "gpt-4":
+ values["context_window"] = 8192
+
+ # Set verbosity defaults for GPT-5 models
+ if model == "gpt-5" and values.get("verbosity") is None:
+ values["verbosity"] = "medium"
+
+ return values
+
@model_validator(mode="before")
@classmethod
def set_default_enable_reasoner(cls, values):
@@ -158,6 +198,16 @@ class LLMConfig(BaseModel):
context_window=256000,
max_tokens=8192,
)
+ elif model_name == "gpt-5":
+ return cls(
+ model="gpt-5",
+ model_endpoint_type="openai",
+ model_endpoint="https://api.openai.com/v1",
+ model_wrapper=None,
+ context_window=128000,
+ verbosity="medium",
+ max_tokens=16384,
+ )
elif model_name == "letta":
return cls(
model="memgpt-openai",
@@ -195,6 +245,11 @@ class LLMConfig(BaseModel):
config.model.startswith("gemini-2.5-flash") or config.model.startswith("gemini-2.5-pro")
)
+ @classmethod
+ def supports_verbosity(cls, config: "LLMConfig") -> bool:
+ """Check if the model supports verbosity control."""
+ return config.model_endpoint_type == "openai" and config.model.startswith("gpt-5")
+
@classmethod
def apply_reasoning_setting_to_config(cls, config: "LLMConfig", reasoning: bool):
if not reasoning:
diff --git a/letta/schemas/openai/chat_completion_request.py b/letta/schemas/openai/chat_completion_request.py
index 270925732..8e7a69b03 100644
--- a/letta/schemas/openai/chat_completion_request.py
+++ b/letta/schemas/openai/chat_completion_request.py
@@ -135,6 +135,7 @@ class ChatCompletionRequest(BaseModel):
user: Optional[str] = None # unique ID of the end-user (for monitoring)
parallel_tool_calls: Optional[bool] = None
instructions: Optional[str] = None
+ verbosity: Optional[Literal["low", "medium", "high"]] = None # For verbosity control in GPT-5 models
# function-calling related
tools: Optional[List[Tool]] = None