Skip to content

Commit 8bc4620

Browse files
NourFahmybaberabb
andauthored
Fix Anthropic API compatibility issues in chat completions (#3054)
* Fix Anthropic API compatibility issues in chat completions solves two important compatibility issues between the LM Eval Harness and Anthropic's API: 1) The type field issue - Anthropic's Messages API doesn't accept the type field that other APIs might expect, that was previously included 2) The stop sequences issue - Anthropic requires stop sequences to contain non-whitespace characters tested with most recent models from anthopic; claude-sonnet-4-0, claude-opus-4-0, resolved my local api errors * pacufy pre-commit * add type --------- Co-authored-by: Baber <[email protected]>
1 parent 68c3a81 commit 8bc4620

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lm_eval/models/anthropic_llms.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,29 @@ def _create_payload(
323323
)
324324
if system:
325325
messages = messages[1:]
326+
327+
cleaned_messages = []
328+
for msg in messages:
329+
cleaned_msg = {
330+
"role": msg["role"],
331+
"content": [
332+
{"type": msg["type"], msg["type"]: msg["content"]},
333+
],
334+
}
335+
cleaned_messages.append(cleaned_msg)
336+
326337
gen_kwargs.pop("do_sample", False)
327338
max_tokens = gen_kwargs.pop("max_gen_toks", self._max_gen_toks)
328339
temperature = gen_kwargs.pop("temperature", 0)
329340
stop = handle_stop_sequences(gen_kwargs.pop("until", ["\n\nHuman:"]), eos=eos)
330341
if not isinstance(stop, list):
331342
stop = [stop]
343+
344+
# Filter out empty or whitespace-only stop sequences for Anthropic API
345+
stop = [s for s in stop if s and s.strip()]
346+
332347
out = {
333-
"messages": messages,
348+
"messages": cleaned_messages,
334349
"model": self.model,
335350
"max_tokens": max_tokens,
336351
"temperature": temperature,

0 commit comments

Comments
 (0)