gemini-agent-framework 0.2.2__py3-none-any.whl → 0.2.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gemini_agent/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  from .agent import Agent
2
2
 
3
- __version__ = "0.2.2"
3
+ __version__ = "0.2.4"
4
4
  __all__ = ["Agent"]
gemini_agent/agent.py CHANGED
@@ -314,7 +314,6 @@ class Agent:
314
314
 
315
315
  def _log_json(self, json_data: Dict[str, Any], file_name: str, debug_scope: Optional[str] = None) -> None:
316
316
  """Logs the JSON data to a file."""
317
- print("in log json")
318
317
  if "json" not in debug_scope:
319
318
  return
320
319
  with open(file_name, "w") as f:
@@ -329,7 +328,7 @@ class Agent:
329
328
  self,
330
329
  user_prompt: str,
331
330
  system_prompt: Optional[str] = None,
332
- response_structure: Optional[Dict[str, Any]] = None,
331
+ json_format: bool = False,
333
332
  conversation_history: Optional[List[Dict[str, Any]]] = None,
334
333
  debug_scope: Optional[str] = [],
335
334
  ) -> Any:
@@ -339,15 +338,14 @@ class Agent:
339
338
  Args:
340
339
  user_prompt: The user's input prompt
341
340
  system_prompt: Optional system prompt to override the default
342
- response_structure: Optional structure to enforce on the response
341
+ json_format: If True, response will be formatted as JSON. Default is False (plain text)
343
342
  conversation_history: Optional list of previous conversation turns
344
343
 
345
344
  Returns:
346
- The model's response, processed according to the response structure if provided
345
+ The model's response, formatted as JSON if json_format is True, otherwise plain text
347
346
  """
348
347
  self._intermediate_results = {}
349
348
 
350
-
351
349
  current_contents = conversation_history if conversation_history else []
352
350
 
353
351
  # Add system instruction to payload
@@ -365,30 +363,15 @@ class Agent:
365
363
  payload["tools"] = [{"functionDeclarations": self._registered_tools_json}]
366
364
  payload["toolConfig"] = {"functionCallingConfig": {"mode": "AUTO"}}
367
365
 
368
- apply_structure_later = bool(response_structure) and bool(self._registered_tools_json)
369
- final_response_schema = None
370
- final_mime_type = None
371
-
372
- if response_structure and not self._registered_tools_json:
373
- apply_structure_later = False
374
- # If response_structure is a string type, make it more flexible
375
- if response_structure.get("type") == "string":
376
- response_structure = {
377
- "type": ["string", "object"],
378
- "properties": {"value": {"type": "string"}},
379
- }
380
- payload["generationConfig"] = {
381
- "response_mime_type": "application/json",
382
- "response_schema": response_structure,
383
- }
384
- final_mime_type = "application/json"
385
- final_response_schema = response_structure
366
+ # Don't set JSON formatting initially if tools are available
367
+ # We'll apply it later after tool calls are completed
368
+ apply_json_format_later = json_format and bool(self._registered_tools_json)
369
+
386
370
  count = 0
387
371
  while True:
388
372
  self._log_json(payload, f"payload_{count}.json", debug_scope)
389
373
  count += 1
390
374
  response_data = self._call_gemini_api(payload, debug_scope)
391
- print("response data " , response_data)
392
375
  if "error" in response_data:
393
376
  self._log_text(
394
377
  f"API call failed: {response_data['error'].get('message', 'Unknown API error')}"
@@ -412,8 +395,6 @@ class Agent:
412
395
  content = candidate["content"]
413
396
 
414
397
  for part in content["parts"]:
415
-
416
-
417
398
  if "functionCall" in part:
418
399
  payload["contents"].append({"role": "model", "parts": [part]})
419
400
  fc = part["functionCall"]
@@ -502,41 +483,32 @@ class Agent:
502
483
  elif "text" in part:
503
484
  final_text = part["text"]
504
485
 
505
- if final_mime_type == "application/json" and final_response_schema:
506
- try:
507
- structured_output = json.loads(final_text)
508
- if not any(
509
- "functionCall" in p
510
- for p in content["parts"][content["parts"].index(part) + 1 :]
511
- ):
512
- return structured_output
513
- except json.JSONDecodeError as e:
514
- self._log_text(
515
- f"Warning: Failed to parse initially structured output: {e}. Continuing with raw text.",
516
- debug_scope
517
- )
518
-
519
- elif apply_structure_later:
520
- if not any(
521
- "functionCall" in p
522
- for p in content["parts"][content["parts"].index(part) + 1 :]
523
- ):
524
- self._log_text("--- Attempting final structuring call ---", debug_scope)
525
- # Include the full conversation history in the formatting payload
486
+ # Check if there are more function calls coming
487
+ has_more_function_calls = any(
488
+ "functionCall" in p
489
+ for p in content["parts"][content["parts"].index(part) + 1 :]
490
+ )
491
+
492
+ if not has_more_function_calls:
493
+ # If JSON format is requested and we have tools, make a final formatting call
494
+ if apply_json_format_later:
495
+ self._log_text("--- Making final JSON formatting call ---", debug_scope)
526
496
  formatting_payload = {
497
+ "system_instruction": {
498
+ "parts": [{"text": system_prompt if system_prompt else ""},{"text": self._get_system_prompt()}]
499
+ },
527
500
  "contents": payload["contents"] + [
528
501
  {
529
502
  "role": "user",
530
503
  "parts": [
531
504
  {
532
- "text": f"Based on our conversation above, please format the following information according to the requested JSON structure:\n\n{final_text}"
505
+ "text": f"Based on our conversation above, please format your response as JSON. Here is the current response: {final_text}"
533
506
  }
534
507
  ],
535
508
  }
536
509
  ],
537
510
  "generationConfig": {
538
- "response_mime_type": "application/json",
539
- "response_schema": response_structure,
511
+ "response_mime_type": "application/json"
540
512
  },
541
513
  }
542
514
  self._log_json(formatting_payload, f"formatting_payload_{count}.json", debug_scope)
@@ -545,7 +517,7 @@ class Agent:
545
517
 
546
518
  if "error" in structured_response_data:
547
519
  self._log_text(
548
- f"Structuring call failed: {structured_response_data['error']}. Returning intermediate text.",
520
+ f"JSON formatting call failed: {structured_response_data['error']}. Returning raw text.",
549
521
  debug_scope
550
522
  )
551
523
  return final_text
@@ -558,57 +530,24 @@ class Agent:
558
530
  return structured_output
559
531
  except (KeyError, IndexError, json.JSONDecodeError) as e:
560
532
  self._log_text(
561
- f"Warning: Failed to parse structured output after formatting call: {e}. Returning intermediate text.",
533
+ f"Warning: Failed to parse JSON response after formatting call: {e}. Returning raw text.",
562
534
  debug_scope
563
535
  )
564
536
  return final_text
565
-
566
- elif not any(
567
- "functionCall" in p
568
- for p in content["parts"][content["parts"].index(part) + 1 :]
569
- ):
570
- if response_structure and not apply_structure_later:
571
- self._log_text("--- Attempting final structuring call ---", debug_scope)
572
- formatting_payload = {
573
- "contents": [
574
- {
575
- "role": "user",
576
- "parts": [
577
- {
578
- "text": f"Please format the following information according to the requested JSON structure:\n\n{final_text}"
579
- }
580
- ],
581
- }
582
- ],
583
- "generationConfig": {
584
- "response_mime_type": "application/json",
585
- "response_schema": response_structure,
586
- },
587
- }
588
- self._log_json(formatting_payload, f"formatting_payload_{count}.json", debug_scope)
589
- count += 1
590
- structured_response_data = self._call_gemini_api(formatting_payload, debug_scope)
591
-
592
- if "error" in structured_response_data:
593
- self._log_text(
594
- f"Structuring call failed: {structured_response_data['error']}. Returning intermediate text."
595
- , debug_scope
596
- )
597
- return final_text
598
-
537
+ elif json_format:
538
+ # Direct JSON formatting (no tools involved)
599
539
  try:
600
- structured_text = structured_response_data["candidates"][0][
601
- "content"
602
- ]["parts"][0]["text"]
603
- structured_output = json.loads(structured_text)
540
+ structured_output = json.loads(final_text)
604
541
  return structured_output
605
- except (KeyError, IndexError, json.JSONDecodeError) as e:
542
+ except json.JSONDecodeError as e:
606
543
  self._log_text(
607
- f"Warning: Failed to parse structured output after formatting call: {e}. Returning intermediate text.",
544
+ f"Warning: Failed to parse JSON response: {e}. Returning raw text.",
608
545
  debug_scope
609
546
  )
610
547
  return final_text
611
- return final_text
548
+ else:
549
+ # Return plain text response
550
+ return final_text
612
551
  continue
613
552
 
614
553
  except (KeyError, IndexError) as e:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gemini-agent-framework
3
- Version: 0.2.2
3
+ Version: 0.2.4
4
4
  Summary: A framework for building agents that use Gemini's function calling capabilities
5
5
  Project-URL: Homepage, https://github.com/m7mdony/gemini-agent-framework
6
6
  Project-URL: Documentation, https://m7mdony.github.io/gemini-agent-framework
@@ -0,0 +1,6 @@
1
+ gemini_agent/__init__.py,sha256=o91YZVXOYTEXZwEBocIH6dE7MBk2JBKy6-qEyl5oFZ0,68
2
+ gemini_agent/agent.py,sha256=Hk4TqameeOxDcbiF3yTIIK1UOSmkFQQelTH67vS74xg,25185
3
+ gemini_agent_framework-0.2.4.dist-info/METADATA,sha256=LY4ne20323PGI4AhgWrg8mQvqFKBNu6BjqdbvcRrszM,5032
4
+ gemini_agent_framework-0.2.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
+ gemini_agent_framework-0.2.4.dist-info/licenses/LICENSE,sha256=NHp9eKeWbB-Fp6Ff24BXSemJOa6UEZa9IAp9U7O9oBM,1073
6
+ gemini_agent_framework-0.2.4.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- gemini_agent/__init__.py,sha256=2220VPV3WqLMo3kDIRmbjzxzY3O_kd0fkkbjjBWJ0-o,68
2
- gemini_agent/agent.py,sha256=ymZLfcB_0W4pcnu8pFQwFRLUR2RpfMtOPTa51lb9gvg,28538
3
- gemini_agent_framework-0.2.2.dist-info/METADATA,sha256=2Nk-YfrwziO79Al0yy2sidfpmBMx1qu4se7osek8mDo,5032
4
- gemini_agent_framework-0.2.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
- gemini_agent_framework-0.2.2.dist-info/licenses/LICENSE,sha256=NHp9eKeWbB-Fp6Ff24BXSemJOa6UEZa9IAp9U7O9oBM,1073
6
- gemini_agent_framework-0.2.2.dist-info/RECORD,,