Skip to content

Commit

Permalink
Fixed use of validate_environment in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vishah02 committed Jan 21, 2025
1 parent c353979 commit f06e804
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions libs/genai/tests/integration_tests/test_chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ async def model_astream(context: str) -> List[BaseMessageChunk]:

def test_output_matches_prompt_keys() -> None:
"""
Validate that when response_mime_type="application/json" is specified,
the output is a valid JSON format and contains the expected structure
Validate that when response_mime_type="application/json" is specified,
the output is a valid JSON format and contains the expected structure
based on the prompt.
"""

Expand Down Expand Up @@ -554,8 +554,9 @@ def test_output_matches_prompt_keys() -> None:
pytest.fail(f"Response is not valid JSON: {e}")

list_key = prompt_key_names["list_key"]
assert list_key in response_data, \
f"Expected key '{list_key}' is missing in the response."
assert (
list_key in response_data
), f"Expected key '{list_key}' is missing in the response."
grocery_items = response_data[list_key]
assert isinstance(grocery_items, list), f"'{list_key}' should be a list."

Expand All @@ -569,21 +570,22 @@ def test_output_matches_prompt_keys() -> None:

print("Response matches the key names specified in the prompt.")


def test_validate_response_mime_type_and_schema() -> None:
"""
Test that `response_mime_type` and `response_schema` are validated correctly.
Ensure valid combinations of `response_mime_type` and `response_schema` pass,
and invalid ones raise appropriate errors.
"""

valid_model = ChatGoogleGenerativeAI(
llm = ChatGoogleGenerativeAI(
model="gemini-1.5-pro",
response_mime_type="application/json",
response_schema={"type": "list", "items": {"type": "object"}}, # Example schema
)

try:
valid_model.validate_environment()
llm.invoke("Hello")
except ValueError as e:
pytest.fail(f"Validation failed unexpectedly with valid parameters: {e}")

Expand All @@ -592,14 +594,14 @@ def test_validate_response_mime_type_and_schema() -> None:
model="gemini-1.5-pro",
response_mime_type="invalid/type",
response_schema={"type": "list", "items": {"type": "object"}},
).validate_environment()
).invoke("Hello")

try:
ChatGoogleGenerativeAI(
model="gemini-1.5-pro",
response_mime_type="application/json",
).validate_environment()
).invoke("Hello")
except ValueError as e:
pytest.fail(
f"Validation failed unexpectedly with a valid MIME type and no schema: {e}"
)
)

0 comments on commit f06e804

Please sign in to comment.