최근 OpenAI API를 structured outputs 모드로 호출하다가, 간헐적으로 이런 에러를 맞닥뜨렸습니다:
openai.LengthFinishReasonError: Could not parse response content as the length limit was reached - CompletionUsage(completion_tokens=16000, prompt_tokens=3501, total_tokens=19501, ...)
아래 글들에서 공통적으로 지적된 포인트는:
- 모델이 반복(loop) 출력을 하거나 structured format이 꼬이면서 불필요하게 길어지는 경우
- 동시 요청(concurrency) 시 내부 토큰 관리가 불안정해지는 경우
- max_tokens 여유 설정이 부족할 때 발생하는 경우
해결 방법: max_tokens 늘리기
여러 시도를 해봤는데, max_tokens 값을 더 크게 잡아주니 문제가 사라졌습니다.
기존에는 응답 상한을 모델 context 한도와 거의 비슷하게 설정했는데, 이를 더 여유 있게 늘려주니 반복되더라도 파싱 실패가 줄어들었습니다.
정리
- LengthFinishReasonError 는 structured output 호출 시 가끔 발생할 수 있다.
- 원인은 반복 출력, strict 포맷 파싱 실패, concurrency 영향 등이 복합적으로 작용한다.
- 실무적 해결책 중 하나는 max_tokens 값을 넉넉히 주는 것이다.
물론 이것만이 유일한 해법은 아닙니다.
- temperature / top_p 조정
- frequency penalty 활용
- stop sequence 지정
- structured output 요구사항 단순화
등도 병행하면 더 안정적인 결과를 얻을 수 있습니다.
https://github.com/vllm-project/vllm/issues/18250
[Bug]: LengthFinishReasonError under high concurrency · Issue #18250 · vllm-project/vllm
Your current environment The output of python collect_env.py INFO 05-15 23:45:35 [__init__.py:239] Automatically detected platform cuda. Collecting environment information... PyTorch version: 2.6.0...
github.com
https://github.com/vllm-project/vllm/issues/8706
[Usage]: Is there any difference between max_tokens and max_model_len? · Issue #8706 · vllm-project/vllm
Your current environment vllm=0.5.4 llm = LLM( model=MODEL_NAME, trust_remote_code=True, gpu_memory_utilization=0.5, max_model_len=2048, tensor_parallel_size=2 ), sampling_params = SamplingParams( ...
github.com