Enhance OpenAI response model with context size and location

Added user_location and search_context_size fields with validation. Implemented context_window adjustment based on search_context_size.
This commit is contained in:
Nathan DeMoss 2025-11-26 14:30:23 -05:00 committed by GitHub
parent d1a7bc36a2
commit 25aeb1fa00
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -421,9 +421,23 @@ class OpenAIResponseInputToolWebSearch(BaseModel):
| Literal["web_search_preview_2025_03_11"] | Literal["web_search_preview_2025_03_11"]
| Literal["web_search_2025_08_26"] | Literal["web_search_2025_08_26"]
) = "web_search" ) = "web_search"
# TODO: actually use search_context_size somewhere... # Context size hint; validated to accepted values. Used to set an internal context window.
search_context_size: str | None = Field(default="medium", pattern="^low|medium|high$") search_context_size: str | None = Field(default="medium", pattern="^low|medium|high$")
# TODO: add user_location # Optional user location hint to improve local relevance (e.g., "Seattle, WA").
user_location: str | None = None
# Derived internal-only: token budget influenced by search_context_size. Excluded from wire schema.
context_window: int = Field(default=1024, exclude=True)
@model_validator(mode="after")
def _apply_context_size(self) -> "OpenAIResponseInputToolWebSearch":
size = (self.search_context_size or "medium").lower()
if size == "low":
object.__setattr__(self, "context_window", 512)
elif size == "high":
object.__setattr__(self, "context_window", 2048)
else:
object.__setattr__(self, "context_window", 1024)
return self
@json_schema_type @json_schema_type