Optimize response string construction

Use list accumulation and join in get_response_string to avoid O(n^2) string concatenation and add a brief comment explaining the performance rationale.
This commit is contained in:
AlexsanderHamir 2025-11-15 17:37:01 -08:00
parent 8ea0e31678
commit 98e2b64040

View file

@ -4437,17 +4437,20 @@ def get_response_string(response_obj: Union[ModelResponse, ModelResponseStream])
responses_api_response = getattr(response_obj, "response", None)
if responses_api_response and hasattr(responses_api_response, "output"):
output_list = responses_api_response.output
response_str = ""
# Use list accumulation to avoid O(n^2) string concatenation:
# repeatedly doing `response_str += part` copies the full string each time
# because Python strings are immutable, so total work grows with n^2.
response_output_parts: List[str] = []
for output_item in output_list:
# Handle output items with content array
if hasattr(output_item, "content"):
for content_part in output_item.content:
if hasattr(content_part, "text"):
response_str += content_part.text
response_output_parts.append(content_part.text)
# Handle output items with direct text field
elif hasattr(output_item, "text"):
response_str += output_item.text
return response_str
response_output_parts.append(output_item.text)
return "".join(response_output_parts)
# Handle Responses API text delta events
if hasattr(response_obj, "type") and hasattr(response_obj, "delta"):
@ -4461,16 +4464,17 @@ def get_response_string(response_obj: Union[ModelResponse, ModelResponseStream])
response_obj.choices
)
response_str = ""
# Use list accumulation to avoid O(n^2) string concatenation across choices
response_parts: List[str] = []
for choice in _choices:
if isinstance(choice, Choices):
if choice.message.content is not None:
response_str += choice.message.content
response_parts.append(str(choice.message.content))
elif isinstance(choice, StreamingChoices):
if choice.delta.content is not None:
response_str += choice.delta.content
response_parts.append(str(choice.delta.content))
return response_str
return "".join(response_parts)
def get_api_key(llm_provider: str, dynamic_api_key: Optional[str]):