ValueError: Could not extract json string from output (with correct output) #181

Open
opened 2026-02-16 00:17:04 -05:00 by yindo · 0 comments
Owner

Originally created by @jakubstrawa1 on GitHub (Jul 11, 2024).

When using LLMTextCompletionProgram to incorporate the pydantic modelling for my output i get a very strange result. The error indicates that the output is proper (134436839ABCDFG), but it still throws an error that it cannot extract json string from output. Any ideas why this is happening?

LLM im using is gpt 3.5 turbo

pip list

llama-cloud                             0.0.6
llama-index                             0.10.54
llama-index-agent-openai                0.1.7
llama-index-cli                         0.1.12
llama-index-core                        0.10.53.post1
llama-index-embeddings-openai           0.1.10
llama-index-indices-managed-llama-cloud 0.2.4
llama-index-legacy                      0.9.48
llama-index-llms-openai                 0.1.25
llama-index-multi-modal-llms-openai     0.1.7
llama-index-postprocessor-cohere-rerank 0.1.7
llama-index-program-openai              0.1.6
llama-index-question-gen-openai         0.1.3
llama-index-readers-file                0.1.30
llama-index-readers-llama-parse         0.1.6
llama-parse                             0.4.6
question= "Extract the 'LEI' for the company from the text. The 'ABC' should be in the format XXXXXXXXXXXXXXXXXXXX, where X can be either a number or an uppercase letter (e.g., 529900QF6F5IB9WQCL03). Please respond with only the LEI and no additional information. Return 'No info' if no ABCis found."

prompt= json_val['prompts']['prompt_template'].format(question=question, context=processed_document)
llm_completion = LLMTextCompletionProgram.from_defaults(
    llm=llm,
    output_cls=ABC,
    prompt_template_str=prompt,
    verbose=True,
)

x = llm_completion()

ValueError: Could not extract json string from output: "ABC": "134436839ABCDFG"

Pydantic model looks like this

class ABC(BaseModel):

    abc: str = Field(..., description="The 'ABC' or 'No info' if it doesn't exist.")

    @validator("abc")
    def validate_lei_code(cls, v):
        if v.lower() != "no info" and not re.match(r"^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}$", v):
            raise ValueError("Invalid ABC code format")
        return v
Originally created by @jakubstrawa1 on GitHub (Jul 11, 2024). When using LLMTextCompletionProgram to incorporate the pydantic modelling for my output i get a very strange result. The error indicates that the output is proper (134436839ABCDFG), but it still throws an error that it cannot extract json string from output. Any ideas why this is happening? LLM im using is gpt 3.5 turbo pip list ``` llama-cloud 0.0.6 llama-index 0.10.54 llama-index-agent-openai 0.1.7 llama-index-cli 0.1.12 llama-index-core 0.10.53.post1 llama-index-embeddings-openai 0.1.10 llama-index-indices-managed-llama-cloud 0.2.4 llama-index-legacy 0.9.48 llama-index-llms-openai 0.1.25 llama-index-multi-modal-llms-openai 0.1.7 llama-index-postprocessor-cohere-rerank 0.1.7 llama-index-program-openai 0.1.6 llama-index-question-gen-openai 0.1.3 llama-index-readers-file 0.1.30 llama-index-readers-llama-parse 0.1.6 llama-parse 0.4.6 ``` ``` question= "Extract the 'LEI' for the company from the text. The 'ABC' should be in the format XXXXXXXXXXXXXXXXXXXX, where X can be either a number or an uppercase letter (e.g., 529900QF6F5IB9WQCL03). Please respond with only the LEI and no additional information. Return 'No info' if no ABCis found." prompt= json_val['prompts']['prompt_template'].format(question=question, context=processed_document) llm_completion = LLMTextCompletionProgram.from_defaults( llm=llm, output_cls=ABC, prompt_template_str=prompt, verbose=True, ) x = llm_completion() ``` ValueError: Could not extract json string from output: "ABC": "134436839ABCDFG" Pydantic model looks like this ``` class ABC(BaseModel): abc: str = Field(..., description="The 'ABC' or 'No info' if it doesn't exist.") @validator("abc") def validate_lei_code(cls, v): if v.lower() != "no info" and not re.match(r"^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}$", v): raise ValueError("Invalid ABC code format") return v ```
yindo added the bug label 2026-02-16 00:17:04 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: run-llama/llama_cloud_services#181